I want to sort data with "id" key, how can I understand what is format string for NSPredicate
format ? I have an 100 number post.
My code :
let objectIDs = posts.map{$0.id}
let predicate = NSPredicate(format: "self IN %@", objectIDs)
let sortByUserId = NSSortDescriptor(key: "id", ascending: true)
I have an error with this description:
can not parse "self IN %@" format string.
Code for Parsing JSON Data:
func postsFromJSONData(data : NSData, inContext context: NSManagedObjectContext) -> PostResult {
do{
let jsonObject : [[String:AnyObject]]
= try NSJSONSerialization.JSONObjectWithData(data, options: []) as! [[String:AnyObject]]
for postJSON in jsonObject {
if let post = postFromJsonObject(postJSON, incontext : context) {
finalPost.append(post)
}
}
return .Sucsess(finalPost)
}
catch let error {
return .Failure(error)
}
}
fetch the main queue posts when the web service finishes:
func fetchRecentPost(completion completion: (PostResult) -> Void){
let request = NSURLRequest(URL: recentPostURL(["":""]))
let task = session.dataTaskWithRequest(request, completionHandler: {
(data, response, error) in
var result = self.processRecentPostRequest(data: data, error: error)
if case let .Sucsess(posts) = result {
let mainQueueContext = self.coreDataStack.mainQueueContext
mainQueueContext.performBlockAndWait(){
try! mainQueueContext.obtainPermanentIDsForObjects(posts)
}
let objectIDs = posts.map{$0.id}
let predicate = NSPredicate(format: "self IN %@", objectIDs)
let sortByUserId = NSSortDescriptor(key: "id", ascending: true)
do{
try self.coreDataStack.saveChanges()
let mainQueuePosts = try self.fetchMainQueuePosts(predicate: predicate, sortDiscriptors: [sortByUserId])
result = .Sucsess(mainQueuePosts)
}
catch let error {
result = .Failure(error)
}
}
completion(result)
})