Start a new topic
Answered

How to take advantage of the Special Keys?

Reading through the querying section and I come across the special keys that I can use to query some data. I'm having trouble getting a working version to query the dates, I know it's mentioned that it is possible to do so with these special keys but I can't find a good example of someone using it. 


Best Answer

Shae,


Following is the working example of using special key for sorting the queried result:


let predicate = NSPredicate(format: "_acl.creator == %@", "5894327d7259704a34axxx7b")
let sortDescriptor = NSSortDescriptor(key: Entity.Key.entityId, ascending: true)
let query = Query(predicate: predicate, sortDescriptors: [sortDescriptor])
                
                dataStore.find(query) { (result: Result<AnyRandomAccessCollection<Book>, Swift.Error>) in
                    switch result {
                    case .success(let books):
                        print("Books fetched are: \(books)")
                        print("Books count is: \(books.count)")
                    case .failure(let error):
                        print("Error: \(error)")
                    }
                }


Please do let me know your usecase for querying. What you are trying to achieve? It will help me better understand your usecase.


Thanks,

Pranav


I should do my engineering homework and it includes using this key to sort items in my query. Thank you very much for your help!

Pranav, 


I'm sorting items by date so I can display the newest item first 

Answer

Shae,


Following is the working example of using special key for sorting the queried result:


let predicate = NSPredicate(format: "_acl.creator == %@", "5894327d7259704a34axxx7b")
let sortDescriptor = NSSortDescriptor(key: Entity.Key.entityId, ascending: true)
let query = Query(predicate: predicate, sortDescriptors: [sortDescriptor])
                
                dataStore.find(query) { (result: Result<AnyRandomAccessCollection<Book>, Swift.Error>) in
                    switch result {
                    case .success(let books):
                        print("Books fetched are: \(books)")
                        print("Books count is: \(books.count)")
                    case .failure(let error):
                        print("Error: \(error)")
                    }
                }


Please do let me know your usecase for querying. What you are trying to achieve? It will help me better understand your usecase.


Thanks,

Pranav

Login or Signup to post a comment