使用NSBatchUpdateRequest将Core Data属性设置为nil

Sen*_*cha 6 core-data ios swift

是否可以将属性设置为nil使用NSBatchUpdateRequest?传递NSNull()propertiesToUpdate不工作:

let unlockRequest = NSBatchUpdateRequest(entityName: "MyEntity")
unlockRequest.predicate = NSPredicate(format: "self in %@", myObjectIDs)
unlockRequest.propertiesToUpdate = ["lockDate": NSNull()]
var error: NSError?
myContext.executeRequest(unlockRequest, error: &error)
if let error = error {
    log.error("Failed to unlock: \(error)")
}
Run Code Online (Sandbox Code Playgroud)

我没有得到任何错误,但它没有清除日期.

我也尝试过设置它NSExpression(forConstantValue: NSNull()),但这也不起作用(forConstantValue参数不接受可选值,所以我不能传递它nil.).

Shm*_*idt 5

nil当前 API 允许作为常量值传递。

unlockRequest.propertiesToUpdate = ["lockDate": NSExpression(forConstantValue: nil)]
Run Code Online (Sandbox Code Playgroud)


Z S*_*Z S 0

这在 Objective-C 中似乎可以正常工作:

NSBatchUpdateRequest *batch = [[NSBatchUpdateRequest alloc] initWithEntityName:entityName];
NSExpression *value = [NSExpression expressionForConstantValue: nil];
batch.propertiesToUpdate = @{@"lockDate": value};
batch.resultType = NSUpdatedObjectsCountResultType;
NSError *error;                
NSBatchUpdateResult *results = [self.privateContext executeRequest:batch error:&error];
Run Code Online (Sandbox Code Playgroud)