Batch delete firestore documents receiving error - ios

I'm trying to run through a loop to batch delete all of a particular event's eventPosts (pictures) however I'm running into the following error:
Cannot convert value of type 'QueryDocumentSnapshot' to expected argument type 'DocumentReference'
I know I'm missing something just not sure what it is, here's the loop:
db.collection("posts").whereField("eventId", isEqualTo: eventId).getDocuments() { (snapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
}
else {
for document in snapshot!.documents {
batch.deleteDocument(document)
}
// Commit the batch
batch.commit() { err in
if let err = err {
print("Error writing batch \(err)")
} else {
print("Batch write succeeded.")
}
}
}

Replace
batch.deleteDocument(document)
with
batch.deleteDocument(document.ref)
As you can see in the official documentation the deleteDocument() method should receive the ref to a document as a parameter.

Related

How to get document id after creation Firestore ios?

Pretty new to Firestore. Is there a way to get ID or reference to a newly created document?
firestore.collection(myCollection).document().setData(["myData": data]) { err in
if let err = err {
print("Error writing document: \(err)")
} else {
print("Document successfully written!")
}
}
Any kind of help is highly appreciated.
You can get it before the document is created, because it's generated on the client when you create the DocumentReference.
let ref = firestore.collection(myCollection).document()
// ref is a DocumentReference
let id = ref.documentID
// id contains the random ID
ref.setData(...)

Cannot filter the data in Firestore

I'm trying to filter data in Firestore and when I print the info to the console I get all documents. The Firestore database is structured as follows:
/Collection
/auto-doc ID
/ hosp : "hosp1"
team : "team1"
(there are more fields in every document)
For testing purposes I only have six documents and two of them have the field I want to filter and print to the console (hosp1).
#IBAction func getData2(_ sender: Any) {
if HOSP != "hosp1" {
query = Firestore.firestore().collection(PTLIST_REF).whereField("hosp", isEqualTo: "hosp1")
ptListCollectionRef.getDocuments { (snapshot, error) in
if let err = error {
debugPrint("error getting data: \(err)")
}
else {
for document in (snapshot?.documents)! {
print(document.data())
}
}
You are getting all the documents because you are calling getDocuments() functions on a collection reference and not on a query. With other words, you aren't applying any filters at all. To be able to filter your data, please change the following line of code:
ptListCollectionRef.getDocuments {/* ... */}
to
query.getDocuments {/* ... */}

Google Firestore - Memory issue with .whereField()

In my app, I have a problem with the memory.
If I write ref.getDocuments() everything works fine. The memory stay at 40mo. However, if I add .whereField("movementID", isEqualTo: PRID) the memory go from 40mo to more than 1Go ...
ref.whereField("movementID", isEqualTo: PRID).getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
if querySnapshot!.isEmpty {
print("EMPTY")
}
for document in querySnapshot!.documents {
let dict = document.data()
}
}
}
I call this function multiple times, so if the function is called 15-20 times the memory goes to 1Go, but if it's more, the app crash.
Do you have an idea about why there is this memory issue, and how I could correct the problem?

callkit reload extension in ios

i am creating app like as trucaller. Everything is completed, only one issue remaining. issue is that how to reload application extension after add new contact number from server in app. first of all, i enter some static number in the array then after i store it in userdefault. i got this by app-groups functionality. i want that when user synchronize their contact in my application, i want reload contact list.
this is my code
manager.reloadExtension(withIdentifier: extensionIdentifer, completionHandler: { error in
print("error \(error?.localizedDescription)")
if let _ = error{
print("A error \(error?.localizedDescription as String!)");
}
})
this is give me error like below
"sqlite3_step for query 'INSERT INTO PhoneNumberBlockingEntry
(extension_id, phone_number_id) VALUES (?, (SELECT id FROM PhoneNumber
WHERE (number = ?)))' returned 19 (2067) errorMessage 'UNIQUE
constraint failed: PhoneNumberBlockingEntry.extension_id,
PhoneNumberBlockingEntry.phone_number_id'"
Jaydeep: Call your new contact web service or synchronize contacts in application and just reload extension as -
CXCallDirectoryManager.sharedInstance.getEnabledStatusForExtension(withIdentifier: "com.compname.sampleapp", completionHandler: { (enabledStatus,error) ->
Void in if let error = error {
print(error.localizedDescription)
}
CXCallDirectoryManager.sharedInstance.reloadExtension(withIdentifier:"com.compname.sampleapp", completionHandler: {
(error) ->
Void in if let error = error {
print(error.localizedDescription)
}
DispatchQueue.main.async {
self.hud?.hide(animated: true)
}
})
print("No error")
})
Let me know still issue getting. I have done this and working fine.

How do I get user record in CloudKit?

I can fetch the userRecordID, but I get an error whenever I try to fetch the record associated with that ID. Any suggestions? Code and error below:
myContainer.fetchUserRecordID { (thisID, thisError) in
if let userError = thisError {
print("DEBUG: error getting user id; \(userError)")
} else {
if let userID = thisID {
self.publicDatabase.fetch(withRecordID: userID, completionHandler: { (fetchedUser, fetchedError) in
if let thisError = fetchedError {
print("DEBUG: error getting user record; \(thisError)")
}
DEBUG: error getting user record; <CKError 0x174045010: "Internal Error" (1/5001); "Couldn't get a PCS object to unwrap encrypted data for field encryptedPublicSharingKey: (null)">
Error in the code, or my fault for trying beta software (iOS 10, Swift 3 & xCode-beta 8.0 beta 2 (8S162m)
From your example it is not clear if self.publicDatabase is equal to myContainer.publicCloudDatabase.
Make sure you fetch it from the same container's database, otherwise it will obviously not work.
myContainer.fetchUserRecordID { userID, error in
guard error == nil else {
print("DEBUG: error getting user id; \(userError)")
return
}
guard let userRecID = userID else {
print("DEBUG: Can't unwrap userID")
return
}
myContainer.publicCloudDatabase.fetch(withRecordID: userRecID, completionHandler: { (fetchedUser, fetchedError) in
//handle the error and the user
print("DEBUG: User fetch FINISHED!", fetchedUser)
})
}
I needed to change from publicDatabase to privateDatabase.
self.publicDatabase.fetch(withRecordID: userID, completionHandler: { (fetchedUser, fetchedError) in
to
self.privateDatabase.fetch(withRecordID: userID, completionHandler: { (fetchedUser, fetchedError) in

Resources