美文网首页
NSAsynchronousFetchRequest, NSBa

NSAsynchronousFetchRequest, NSBa

作者: 伽蓝香 | 来源:发表于2015-11-27 10:07 被阅读484次

批处理查询数据


    let batchUpdate = NSBatchUpdateRequest(entityName: "Venue")
    batchUpdate.propertiesToUpdate = ["favorite" : NSNumber(bool: true)]
    batchUpdate.affectedStores = coreDataStack.context.persistentStoreCoordinator!.persistentStores
    batchUpdate.resultType = .UpdatedObjectsCountResultType
    
    do {
        let batchResult =
        try coreDataStack.context.executeRequest(batchUpdate) as! NSBatchUpdateResult
        print("Records updated \(batchResult.result!)")
    } catch let error as NSError {
        print("Could not update \(error), \(error.userInfo)")
    } 
     

多线程查询数据


    fetchRequest = NSFetchRequest(entityName: "Venue")
    asyncFetchRequest = NSAsynchronousFetchRequest(fetchRequest: fetchRequest, completionBlock: {  
    [unowned self] (result: NSAsynchronousFetchResult!) -> Void in
        self.venues = result.finalResult as! [Venue]
        self.tableView.reloadData()
    })
    do {
            
        try coreDataStack.context.executeRequest(asyncFetchRequest)
            
    } catch let error as NSError {
        print("could not fetch \(error), \(error.userInfo)")
    }  

用NSAsynchronousFetchRequest包裹NSFetchRequest
执行更新的是使用executeRequest

相关文章

网友评论

      本文标题:NSAsynchronousFetchRequest, NSBa

      本文链接:https://www.haomeiwen.com/subject/ynhdhttx.html