- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
这是我的NSBatchUpdateRequest
:
let batchUpdateRequest = NSBatchUpdateRequest(entityName: "WLItem")
batchUpdateRequest.predicate = NSPredicate(format: "source != rt")
batchUpdateRequest.propertiesToUpdate = ["position": 2]
batchUpdateRequest.resultType = NSBatchUpdateRequestResultType.UpdatedObjectsCountResultType
do {
let batchUpdateResult = try NSManagedObjectContext.MR_defaultContext().executeRequest(batchUpdateRequest) as! NSBatchUpdateResult
} catch {
print("error: \(error)")
}
控制台输出为:
error: NilError
怎么了? XCode7有问题吗?我是第一次使用 NSBatchUpdateRequest
,所以我不知道以前是不是这样。对我来说,该代码应该可以正常工作。
最佳答案
您必须刷新 managedContextObject 才能更新数据库。我创建了一个函数,其中使用 NSBatchUpdateRequest 更新数据库。下面是函数:-
let managedContext = SharedInstance.appDelegate?.persistentContainer.viewContext
func updateDataFromTable(tableName:String, idOfPaticularTable ID:String, updatedDict:Dictionary<String, String>, success:@escaping (_ response:Bool)-> Void, failure:@escaping (_ error: Error?) -> Void) {
let batchRequest = NSBatchUpdateRequest(entityName: tableName)
let predicate = NSPredicate(format: "id == %@", ID)
batchRequest.predicate = predicate
batchRequest.propertiesToUpdate = updatedDict
batchRequest.resultType = .updatedObjectIDsResultType
do {
// Execute Batch Request
let batchUpdateResult = try managedContext?.execute(batchRequest) as! NSBatchUpdateResult
// Extract Object IDs
let objectIDs = batchUpdateResult.result as! [NSManagedObjectID]
for objectID in objectIDs {
// Turn Managed Objects into Faults
let managedObject = managedContext?.object(with: objectID)
managedContext?.refresh(managedObject!, mergeChanges: false)
}
success(true)
} catch let error as NSError {
print(error)
failure(error)
}
}
struct SharedInstance {
static let appDelegate = UIApplication.shared.delegate as? AppDelegate
static let employeetable = "EmployeeModel"
}
关于ios - NSBatchUpdateRequest 在 Swift 中引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32440803/
我正在尝试使用 NSBatchUpdateRequest 来更新一对多关系。 我的实体Presentation有多个Slide,是另一个实体,它们是反比关系。 当我尝试更新幻灯片时,出现此错误: **
我在尝试执行 NSBatchUpdateRequest 时不断收到以下错误。 *** Terminating app due to uncaught exception 'NSInternalInco
目前,我通过以下代码执行多个更新操作。 func updateOrders(_ updates : [(objectID: NSManagedObjectID, order: Int64)]) {
我正在学习 Swift 的同时构建一个博客阅读器。在此,我将数据保存到核心数据,并可以选择将文章标记为“已读”。当我使用 editActionsForRowsAtIndexPath 将它们单独标记为已
长话短说 显然,当通过执行 NSBatchUpdateRequest 发生更改时,NSFetchedResultsController 不知道给定 NSManagedObjectContext 上的数
这是我的NSBatchUpdateRequest: let batchUpdateRequest = NSBatchUpdateRequest(entityName: "WLItem")
是否可以使用 NSBatchUpdateRequest 将属性设置为 nil?将 NSNull() 传递给 propertiesToUpdate 无效: let unlockRequest = NSB
我创建了简单的 NSBatchUpdateRequest: let bur = NSBatchUpdateRequest(entityName: "WLWishlist") bur.predicate
我得到了一个 NSFetchedResultsController,它是我使用 NSManagedObjectContext 设置的。我使用此上下文执行提取。 我还有一个 NSBatchUpdateR
是否可以使用NSBatchUpdateRequest将属性设置为nil?将NSNull()传递给PropertiesToUpdate不起作用:。我没有收到任何错误,但它没有清除日期。。我还尝试将其设置
我是一名优秀的程序员,十分优秀!