这是我的第一个问题:)
好的,所以我通过cocoapods获得了ReskKit 0.23.3的专业版。我使用RestKit / CoreData。
我获取一个URL,结果被映射到我的对象,并被核心数据正确保存。我想使用键值验证来检查一些已经保留的值。我读到我可以在validateKey:error:
上使用方法NSManagedObject
。不知何故,它永远不会被称为。我很沮丧......
这是我的文件(为简单起见,我将逻辑代码连接到一个平面文件中):
JSON响应/集合/ {id}
{
"id": "00000000-0000-0000-0000-00000000000",
"image_url": "http://server/image.png",
"name": "Collection C",
"etag": 42,
"ctag": 42
}
Collection.h
@interface Collection : NSManagedObject
@property(nonatomic, strong) NSString *collectionId;
@property(nonatomic, strong) NSString *name;
@property(nonatomic, strong) NSURL *imageUrl;
@property(nonatomic, strong) NSNumber *etag;
@property(nonatomic, strong) NSNumber *ctag;
@end
Collection.m
@implementation Collection
@dynamic collectionId, name, imageUrl, etag, ctag;
- (BOOL)validateCollectionId:(id *)ioValue error:(NSError **)outError {
NSLog(@"Validating id");
NSLog(@"Coredata collection id: %@", self.collectionId);
NSLog(@"GET collection id: %@", (NSString *)*ioValue);
return YES;
}
- (BOOL)validateEtag:(id *)ioValue error:(NSError **)outError {
NSLog(@"Validating etag");
NSLog(@"Coredata collection etag: %@", self.etag);
NSLog(@"GET collection etag: %@", (NSString *)*ioValue);
return YES;
}
@end
代码
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
[managedObjectStore createPersistentStoreCoordinator];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingString:@"/MyApp.sqlite"];
NSError *error;
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];
NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);
[managedObjectStore createManagedObjectContexts];
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];
[RKManagedObjectStore setDefaultStore:managedObjectStore];
NSURL *url = [NSURL URLWithString:@"http://server/api"];
RKObjectManager *objectManager = [self managerWithBaseURL:url];
objectManager.requestSerializationMIMEType = RKMIMETypeJSON;
objectManager.managedObjectStore = [RKManagedObjectStore defaultStore];
RKEntityMapping *collectionMapping = [RKEntityMapping mappingForEntityForName:@"Collection" inManagedObjectStore:[RKManagedObjectStore defaultStore]];
[collectionMapping addAttributeMappingsFromDictionary:@{@"id": @"collectionId",
@"image_url": @"imageUrl"}];
[collectionMapping addAttributeMappingsFromArray:@[@"name", @"etag", @"ctag"]];
[collectionMapping setIdentificationAttributes:@[@"collectionId"]];
RKResponseDescriptor *collectionResponseDescriptors = [RKResponseDescriptor responseDescriptorWithMapping:collectionMapping
method:RKRequestMethodGET pathPattern:@"collections/:collectionId"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:collectionResponseDescriptor];
[objectManager getObjectsAtPath:@"collections/00000000-0000-0000-0000-00000000000" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
Collection *collection = (Collection *)[mappingResult.array firstObject];
NSLog(@"Collection: %@", collection);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Oh noes :(");
}];
输出
2014-09-13 12:39:04.242 MyApp[41958:607] I restkit:RKLog.m:33 RestKit logging initialized...
2014-09-13 12:39:05.028 MyApp[41890:607] Collection: <NSManagedObject: 0x9108a60> (entity: Collection; id: 0x94166d0 <x-coredata://6645F428-7631-45F0-A8AF-E2352C50F35E/Collection/p1> ; data: {
collectionId = "00000000-0000-0000-0000-00000000000";
ctag = 42;
etag = 42;
imageUrl = "http://server/image.png";
name = "Collection C";
})
所以,我得到了我的Log with the Collection,但validate<Key>:error:
方法中的NSLog都没有被触发......无法找出原因!
修改
有一些休息,我认为这是RKMappingOperation谁负责在我的对象上调用那些验证方法。确切地说,它是validateValue:atKeyPath:
RKMappingOperation.m
...
- (BOOL)validateValue:(id *)value atKeyPath:(NSString *)keyPath
{
BOOL success = YES;
if (self.objectMapping.performsKeyValueValidation && [self.destinationObject respondsToSelector:@selector(validateValue:forKeyPath:error:)]) {
NSError *validationError;
success = [self.destinationObject validateValue:value forKeyPath:keyPath error:&validationError];
...
}
...
但self.destinationObject
是NSManagedObject
而不是Collection
对象......
控制台
(lldb) po [self.destinationObject class]
NSManagedObject
我希望你能以正确的方式引导我:)谢谢!
答案 0 :(得分:1)
您似乎未指定实体应在Core Data模型中使用Collection
类。如果您没有指定任何内容,则默认情况下将使用NSManagedObject
。