【发布时间】:2014-04-29 04:02:41
【问题描述】:
我在 Core Data 中有一个保存的对象(持久化)。让我们说下面是值:
//Entity: employee
objectID: 1111
firstName: @"Jon"
lastName: @"D"
modified: @"10:45PM"
现在,我发出RKManagedObjectRequestOperation *operation 请求。
我设置operation.savesToPersistentStore = NO; 并开始操作。
对象已下载并已修改为以下内容:
//Entity: employee
objectID: 1111
firstName: @"Jonathan"
lastName: @"Doe"
modified: @"10:55PM"
//I have 15 other properties that are either Strings, NSDate, NSNumber, and BOOLs
我相信此时修改后的对象在managedObjectContext
如果Context 中的modified 日期不同,我想将Context 中的每个attribute 与Core Data 中保留的日期进行比较。如果Context attribute 与持久化的不同,我需要为每个属性触发一个标志。一旦所有属性都被破解,我可以通过保存上下文来覆盖对象。
我想我必须手动检查:
if (!([objectInCoreData valueForKey:@"firstName"] isEqualToString:[objectInContext valueForKey@"firstName"]))
{
firstNameValueChange = YES; // Trigger this flag
// I have to trigger a flag for each attribute that's changed to show
// the changed value in the tableView in a different color
}
//Continue to check for each attribute, and at end overwrite persisted with Context by
[self.managedObjectContext save:&error];
问题 1:我觉得有更好的方法来做到这一点。最佳做法是什么?
如何简化检查 15 个不同属性的代码?
问题 2:我在哪里测试每个属性是否已更改?在successBlock里面?将保存:方法?
** 更新 *
RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc]initWithRequest:request responseDescriptors:@[responseDescriptor]];
operation.managedObjectContext = self.objectManager.managedObjectStore.mainQueueManagedObjectContext;
operation.managedObjectCache = appDelegate.managedObjectStore.managedObjectCache;
operation.savesToPersistentStore = NO;
[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSSet *updatedObjects = [self.managedObjectContext updatedObjects];
NSLog(@"updatedObjects Count %i", updatedObjects.count);
NSArray *_updatedObjects = updatedObjects.allObjects;
NSLog(@"_updatedObjects Count %i", _updatedObjects.count);
for (int i = 0; i<_updatedObjects.count; i++)
{
Invite *invite = [_updatedObjects objectAtIndex:i];
NSDictionary *changedValues = [invite changedValues];
}
}failure:^(RKObjectRequestOperation *operation, NSError *error) {}
日志
I restkit.network:RKObjectRequestOperation.m:250 GET 'http://www.domain.com/api?lastrequest=2014-04-22T07%3A06%3A34Z' (200 OK / 28 objects) [request=0.1140s mapping=0.0865s total=0.3034s]
2014-04-29 07:06:43.112 App[10627:60b] updatedObjects Count 0
2014-04-29 07:06:43.112 App[10627:60b] _updatedObjects Count 0
我可以看到我正在获取一些对象的日志,但一切都是 null 或 0。我猜我的 MOC 是空白的。对象如何从mappingResult 到MOC?
【问题讨论】:
标签: ios core-data restkit restkit-0.20 key-value-coding