2014-09-13 90 views
1

這是我在這裏的第一個問題:)RestKit KVC驗證不會調用驗證方法

行,所以我必須通過的CocoaPods ReskKit 0.23.3一個謨。我使用RestKit/CoreData。

我獲取一個URL,結果映射到我的對象,並通過核心數據正確保存。我想使用Key-Value Validation來檢查一些已經存在的值。我讀到我可以在我的NSManagedObject上使用方法validateKey:error:。不知何故,它永遠不會被調用。我很沮喪......

這裏是我的文件(爲簡單起見,我級聯邏輯代碼到這裏一個平面文件):

JSON響應/收藏/(編號)

{ 
    "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"; 
}) 

所以,我得到我的,收集日誌,但在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.destinationObjectNSManagedObject而不是Collection對象...

控制檯

(lldb) po [self.destinationObject class] 
NSManagedObject 

我希望你能帶我以正確的方式:)謝謝!

回答

1

看來您並未指定該實體應在Core Data模型中使用Collection類。如果您沒有指定任何內容,則默認使用NSManagedObject

Demo image from the web

+0

該死!我缺乏CoreData的經驗......謝謝先生! :) – 2014-09-15 07:45:46