CKQueryOperation queryCompletionBlock return a nil cursor - ios

I'm fetching a CloudKit database with CKQueryOperation. For some reason every time when I press a fetch button, the first time I get a nil cursor. The second time it fetches and gets data, it's all good. When I check the recordFetchedBlock it does get the results and appends them, but at the end the array is empty. I don't understand why this happens. I want to show the results immediately since they have been fetched. I think the problem is with the nil cursor, but I'm open for other suggestions. Here's my code:
public class CloudKitDatabase {
static let shared = CloudKitDatabase()
var records = [CKRecord]()
let publicData = CKContainer.default().publicCloudDatabase
init() {
self.fetchRecords()
}
func fetchRecords() {
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "OECD", predicate: predicate)
let queryOperation = CKQueryOperation(query: query)
queryOperation.recordFetchedBlock = {
record in
self.records.append(record)
}
queryOperation.queryCompletionBlock = { cursor, error in
DispatchQueue.main.async {
if error != nil {
print(error.debugDescription)
} else {
if cursor != nil {
self.queryServer(cursor!)
} else {
print("CURSOR IS NIL")
}
}
}
}
self.publicData.add(queryOperation)
}
func queryServer(_ cursor: CKQueryOperation.Cursor) {
let queryOperation = CKQueryOperation(cursor: cursor)
queryOperation.recordFetchedBlock = {
record in
self.records.append(record)
}
queryOperation.queryCompletionBlock = { cursor, error in
DispatchQueue.main.async {
if error != nil {
print(error.debugDescription)
} else {
if cursor != nil {
self.queryServer(cursor!)
} else {
print("CURSOR IS NIL")
}
}
}
}
self.publicData.add(queryOperation)
}
The Debug area tells me that:
CURSOR IS NIL
and CloudKitDatabase.shared.records.isEmpty is true

First try some configs on the first query;
let queryOperation = CKQueryOperation(query: query)
queryOperation.queuePriority = .veryHigh
queryOperation.resultsLimit = 99 // built in limit is 400
Next, don't do the cursor calls in a dispatch and include your completions;
queryOperation.queryCompletionBlock =
{ cursor, error in
if error != nil {
print(error.debugDescription)
} else {
if cursor != nil {
self.queryServer(cursor!)
} else {
print("CURSOR IS NIL")
completion(nil)
}
}
}
and;
queryOperation.queryCompletionBlock =
{ cursor, error in
if error != nil {
print(error.debugDescription)
} else {
if cursor != nil {
self.queryServer(cursor!)
} else {
print("CURSOR IS NIL")
completion(nil)
}
}
}
also don't forget to empty your records array at the beginning of fetchRecords otherwise successive calls will get the same records in the array.

Related

How can we paginate a firebase / firestore query on ios swift?

I try to find a solution for paginate a firebase query on ios/swift but I couldn't build algorithm for my state.
My method is like this:
func downloadData(completion: #escaping ([Post]) -> Void) {
// download data with pagination
let firestoreDatabase = Firestore.firestore()
var first = firestoreDatabase.collection("posts").order(by: "date", descending: true).limit(to: 5)
first.addSnapshotListener{ snapshot, error in
guard let snapshot = snapshot else {
print("Error retrieving cities: \(error.debugDescription)")
return
}
guard let lastSnapshot = snapshot.documents.last else {
// The collection is empty.
return
}
self.postList.removeAll(keepingCapacity: false)
DispatchQueue.global().async {
for document in snapshot.documents {
// getting data from document stuff ...
self.postList.append(self.post)
}
completion(self.postList)
}
// how can I repeat this query as long as lastSnapshot exist
firestoreDatabase.collection("posts").order(by: "date", descending: true).start(afterDocument: lastSnapshot).addSnapshotListener { querySnapshot, error in
}
}
}
I tried following mindset but it didn't work, and entered an infinite loop. I didn't understand why it is.
func downloadData(completion: #escaping ([Post]) -> Void) {
// download data with pagination
let firestoreDatabase = Firestore.firestore()
var first = firestoreDatabase.collection("posts").order(by: "date", descending: true).limit(to: 5)
first.addSnapshotListener{ snapshot, error in
guard let snapshot = snapshot else {
print("Error retrieving cities: \(error.debugDescription)")
return
}
guard let lastSnapshot = snapshot.documents.last else {
// The collection is empty.
return
}
self.postList.removeAll(keepingCapacity: false)
DispatchQueue.global().async {
for document in snapshot.documents {
// geting data from document stuff ...
self.postList.append(self.post)
}
completion(self.postList)
}
repeat {
firestoreDatabase.collection("posts").order(by: "date", descending: true).start(afterDocument: lastSnapshot).addSnapshotListener { querySnapshot, error in
guard let snapshot = snapshot else {
print("Error retrieving cities: \(error.debugDescription)")
return
}
guard let lastSnapshot = snapshot.documents.last else {
// The collection is empty.
return
}
self.postList.removeAll(keepingCapacity: false)
DispatchQueue.global().async {
for document in snapshot.documents {
// getting data from document stuff ...
self.postList.append(self.post)
}
completion(self.postList)
}
lastSnapshot = snapshot.documents.last
}
} while(lastSnapshot.exists)
}
}
I think lastSnapshot must be nil after the query loop but it is appear that it is still exist.
how can I fix lastSnapshot problem? Or is there different mindset / easiest way to paginate?
In firebase documents, it says just use this but how can we repeat query that has " .start(afterDocument: lastSnapshot) " stuff?
First and foremost, for plain-vanilla pagination, don't use a snapshot listener when fetching documents. You can paginate documents with a snapshot listener but the process is more complex.
I've embedded my notes into the comments in the code below for clarity.
let pageSize = 5
var cursor: DocumentSnapshot?
func getFirstPage(completion: #escaping (_ posts: [Post]?) -> Void) {
let db = Firestore.firestore()
let firstPage = db.collection("posts").order(by: "date", descending: true).limit(to: pageSize)
firstPage.getDocuments { snapshot, error in
guard let snapshot = snapshot else {
// Don't leave the caller hanging on errors; return nil,
// return a Result, throw an error, do something.
completion(nil)
if let error = error {
print(error)
}
return
}
guard !snapshot.isEmpty else {
// There are no results and so there can be no more
// results to paginate; nil the cursor.
cursor = nil
// And don't leave the caller hanging, even on no
// results; return an empty array.
completion([])
return
}
// Before parsing the snapshot, manage the cursor.
if snapshot.count < pageSize {
// This snapshot is smaller than a page size and so
// there can be no more results to paginate; nil
// the cursor.
cursor = nil
} else {
// This snapshot is a full page size and so there
// could potentially be more results to paginate;
// set the cursor.
cursor = snapshot.documents.last
}
var posts: [Post] = []
for doc in snapshot.documents {
posts.append(newPost) // pseudo code
}
completion(posts)
}
}
func continuePages(completion: #escaping (_ posts: [Post]?) -> Void) {
guard let cursor = cursor else {
return
}
let db = Firestore.firestore()
let nextPage = db.collection("posts").order(by: "date", descending: true).limit(to: pageSize).start(afterDocument: cursor)
nextPage.getDocuments { snapshot, error in
guard let snapshot = snapshot else {
completion(nil)
if let error = error {
print(error)
}
return
}
guard !snapshot.isEmpty else {
// There are no results and so there can be no more
// results to paginate; nil the cursor.
cursor = nil
completion([])
return
}
// Before parsing the snapshot, manage the cursor.
if snapshot.count < pageSize {
// This snapshot is smaller than a page size and so
// there can be no more results to paginate; nil
// the cursor.
cursor = nil
} else {
// This snapshot is a full page size and so there
// could potentially be more results to paginate;
// set the cursor.
cursor = snapshot.documents.last
}
var morePosts: [Post] = []
for doc in snapshot.documents {
morePosts.append(newPost) // pseudo code
}
completion(morePosts)
}
}

Firebase Document will not delete, probably a timing error, swift

I'm trying to load data into a tableview. Once a user clicks on the cell, it should delete. However, nothing happens. I know that it runs, error is nil, I know that the document ID exists, so I'm unsure as to why the document isn't deleting. Here is some code:
Here's where i load in my data:
self.comments = []
for index in 0...1
{
var postCategory = ""
if index == 0 { postCategory = "cPosts" }
else { postCategory = "rPosts"}
db.collection(postCategory).order(by: "rankValue", descending: true).getDocuments
{ snapshot, error in
guard error == nil else { print(error!); return }
for postDoc in snapshot!.documents
{
self.getUsername(uid: self.uid!)
{ username in
self.secondGroup.enter()
self.ref = self.db.collection(postCategory).document(postDoc.documentID).collection("comments")
self.ref?.whereField("username", isEqualTo: username).getDocuments
{ snapshot, error in
guard error == nil else { print(error!); return }
for doc in snapshot!.documents
{
let comment = Comment(data: doc.data())
comment.docID = doc.documentID
self.comments.append(comment)
self.firstGroup.enter()
self.repRef = self.db.collection(postCategory).document(postDoc.documentID).collection("comments").document(comment.docID).collection("replies")
self.repRef?.whereField("username", isEqualTo: username).getDocuments
{ snapshot, error in
guard error == nil else { print(error!); return }
for doc in snapshot!.documents
{
let reply = Reply(data: doc.data())
reply.docID = doc.documentID
self.comments.append(reply)
}
self.firstGroup.leave()
}
}
self.firstGroup.notify(queue: .main) {
self.secondGroup.leave()
}
self.secondGroup.notify(queue: .main) {
completion(self.ref!, self.repRef!)
}
}
}
}
}
}
Here's where I actually delete the document:
self.ref?.document(self.comments[indexPath.row].docID).collection("replies").getDocuments
{ snapshot, error in
guard error == nil else { print(error!); return }
for doc in snapshot!.documents
{
doc.reference.delete { error in
guard error == nil else { print(error!); return }
}
}
}
self.ref?.document(self.comments[indexPath.row].docID!).delete { error in
guard error == nil else { print(error!); return }
print()
}
self.repRef?.document(self.comments[indexPath.row].docID).delete { error in
print("ranhere also")
guard error == nil else { print(error!); return }
}

Optional Still Returning Nil After Assigning Value

I am working on a similar feature to 'liking/unliking a post'.
I have an MVVM architecture as;
struct MyStructModel {
var isLiked: Bool? = false
}
class MyStructView {
var isLiked: Bool
init(myStructModel: MyStructModel) {
self.isLiked = myStructModel.isLiked ?? false
}
}
I successfully get the value of whether the post is liked or not here;
func isPostLiked(documentID: String, completion: #escaping (Bool) -> Void) {
guard let authID = auth.id else { return }
let query = reference(to: .users).document(authID).collection("liked").document(documentID)
query.getDocument { (snapshot, error) in
if error != nil {
print(error as Any)
return
}
guard let data = snapshot?.data() else { return }
if let value = data["isLiked"] as? Bool {
completion(value)
} else {
completion(false)
}
}
}
func retrieveReviews(completion: #escaping([MyStructModel]) -> ()) {
var posts = [MyStructModel]()
let query = reference(to: .posts).order(by: "createdAt", descending: true)
query.getDocuments { (snapshot, error) in
if error != nil {
print(error as Any)
return
}
guard let snapshotDocuments = snapshot?.documents else { return }
for document in snapshotDocuments {
if var post = try? JSONDecoder().decodeQuery(MyStructModel.self, fromJSONObject: document.decode()) {
// isLiked is nil here...
self.isPostLiked(documentID: post.documentID!) { (isLiked) in
post.isLiked = isLiked
print("MODEL SAYS: \(post.isLiked!)")
// isLiked is correct value here...
}
posts.append(post)
}
completion(posts)
}
}
}
However, when it gets to my cell the value is still nil.
Adding Cell Code:
var post: MyStructView? {
didSet {
guard let post = post else { return }
print(post.isLiked!)
}
}
Your isLiked property is likely nil in your cells because the retrieveReviews function doesn't wait for the isPostLiked function to complete before completing itself.
You could easily solve this issue by using DispatchGroups. This would allow you to make sure all of your Posts have their isLiked value properly set before being inserted in the array, and then simply use the DispatchGroup's notify block to return all the loaded posts via the completion handler:
func retrieveReviews(completion: #escaping([MyStructModel]) -> ()) {
var posts = [MyStructModel]()
let query = reference(to: .posts).order(by: "createdAt", descending: true)
query.getDocuments { [weak self] (snapshot, error) in
guard let self = self else { return }
if error != nil {
return
}
guard let documents = snapshot?.documents else { return }
let dispatchGroup = DispatchGroup()
for document in documents {
dispatchGroup.enter()
if var post = try? JSONDecoder().decodeQuery(MyStructModel.self, fromJSONObject: document.decode()) {
self.isPostLiked(documentID: post.documentID!) { isLiked in
post.isLiked = isLiked
posts.append(post)
dispatchGroup.leave()
}
}
}
dispatchGroup.notify(queue: .main) {
completion(posts)
}
}
}

Returning name from Firestore?

I'm trying to return a name after getting it on Firestore, but for some reason it's not working.
Here's my code:
func getName() -> String {
var name = ""
db.collection("users").whereField("email", isEqualTo: user.email!).getDocuments { (snapshot, error) in
if error != nil {
print(error!)
} else {
for document in (snapshot?.documents)! {
name = document.data()["name"] as! String
// if I add `print(name) here, it works.`
}
}
}
return name
}
But it returns an empty string :/ I want to return the actual name. How do I fix this?
getDocuments is an asynchronous function. This means the name variable doesn't wait for the function to complete before continue executing. If you want to return the returned name from the document, you can take a look at the following code:
func getName(_ completion: (String) -> ()) {
db.collection("users").whereField("email", isEqualTo: user.email!).getDocuments { (snapshot, error) in
if error != nil {
print(error!)
} else {
for document in (snapshot?.documents)! {
name = document.data()["name"] as! String
completion(name)
}
}
}
}
getName { name in
print(name)
}

Add unique values to my array in Parse Server (swift 3)

My Parse Server has a class called "BooksAddedByUser", which has 3 columns:
objectId
user - contains the username of the PFUser.current()
ISBNArray - the ISBN of books added by the user
I would like to add the newly added bookNumber into the ISBNArray only if it doesn't exist in the that array yet. However, whenever I run my code below, it creates new objectIds with the same username in the class "BooksAddedByUser". And it also doesn't check if the bookNumber already exists. I'm not sure what's going on honestly.
let booksAddedByUser = PFObject(className: "BooksAddedByUser")
booksAddedByUser["user"] = PFUser.current()?.username
let query = PFQuery(className: "BooksAddedByUser")
query.whereKey("ISBNArray", contains: bookNumber)
query.findObjectsInBackground { (objects, error) in
if error != nil {
print(error)
} else {
print(self.bookNumber)
if let objects = objects {
for object in objects {
print("book is already added i think")
}
}
}
}
booksAddedByUser.addObjects(from: [bookNumber], forKey: "ISBNArray")
booksAddedByUser.saveInBackground { (success, error) in
if error != nil {
print("error saving new book")
} else {
print("new book saved!")
}
}
EDIT w/ new code:
let booksAddedByUser = PFObject(className: "BooksAddedByUser")
booksAddedByUser["user"] = PFUser.current()?.username
let query = PFQuery(className: "BooksAddedByUser")
query.findObjectsInBackground { (objects, error) in
if error != nil {
print(error)
} else {
print(self.bookNumber)
if let objects = objects {
if objects.contains(bookNumber) {
print("book exists")
}
}
}
}
booksAddedByUser.addObjects(from: [bookNumber], forKey: "ISBNArray")
booksAddedByUser.saveInBackground { (success, error) in
if error != nil {
print("error saving new book")
} else {
print("new book saved!")
}
}
You can check if an object exits in array by using this:
if arrObjects.contains(where: { $0.bookNumberPropertyName == "bookNumber" }) {
print("Book exists")
}

Resources