2012-12-05 37 views
19

我遇到了一個我似乎無法找到解決方法的新問題...下面是我的RestKit代碼,遵循Twitter核心數據示例:RestKit 0.20 - 核心數據:錯誤:無法調用NSManagedObject類上的指定初始值設定項

// 
// RESTKIT 
// 

// restkit object manager 
NSString *baseURL = @"http://test1.website.com"; 
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:baseURL]]; 

// enable activity indicator 
[AFNetworkActivityIndicatorManager sharedManager].enabled = YES; 

// managed object model 
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"AppName" withExtension:@"momd"]; 
NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; 
// NSLog(@"managed object model: %@", managedObjectModel); 

// managed object store 
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel]; 
objectManager.managedObjectStore = managedObjectStore; 
// NSLog(@"managed object store: %@", managedObjectStore); 

// persistent store 
NSError *error; 
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"AppName.sqlite"]; 
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error]; 
if (error) { 
    NSLog(@"unresolved error %@, %@", error, [error userInfo]); 
    abort(); 
} 
// NSLog(@"persistent store coordinator: %@", managedObjectStore.persistentStoreCoordinator); 
// NSLog(@"persistent store: %@", persistentStore); 

// managed object contexts 
[managedObjectStore createManagedObjectContexts]; 
// NSLog(@"managed object context: %@", managedObjectStore.mainQueueManagedObjectContext); 

// managed object cache 
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.mainQueueManagedObjectContext]; 
// NSLog(@"mangaged object cache: %@", managedObjectStore.managedObjectCache); 

// 
// entity mapping 
// 

// album entity mapping 
RKEntityMapping *albumEntityMapping = [RKEntityMapping mappingForEntityForName:@"Album" inManagedObjectStore:managedObjectStore]; 
albumEntityMapping.identificationAttributes = @[ @"identifier" ]; 
[albumEntityMapping addAttributeMappingsFromDictionary:@{ @"title" : @"title" }]; 

// 
// response descriptor 
// 

// album response descriptor 
[objectManager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:albumEntityMapping pathPattern:[NSString stringWithFormat:@"/albums/display/%@/%@", userInfo[@"userID"], userInfo[@"apiKey"]] keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]]; 

我通過一個UITableViewController類請求來自API的信息:

- (void)loadObjectsFromResourcePath:(NSString *)resourcePath; 
{ 
    [[RKObjectManager sharedManager] getObjectsAtPath:resourcePath parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { 
     NSLog(@"mapping result: %@", [mappingResult array]); 
     [self stopLoading]; 
    } failure:^(RKObjectRequestOperation *operation, NSError *error) { 
     NSLog(@"error: %@", error); 
     [self stopLoading]; 
    }]; 
} 

然而,與此代碼我收到以下錯誤:

2012-12-05 17:08:14.830 AppName Alpha[1310:907] I restkit:RKLog.m:34 RestKit logging initialized... 
2012-12-05 17:08:15.107 AppName Alpha[1310:907] resource path: albums/display/102/drrrbSAt6RFLsgHaKiiB6kBIyYRyzxLYinL84Kba 
2012-12-05 17:08:15.113 AppName Alpha[1310:907] I restkit.support:RKMIMETypeSerialization.m:115 JSON Serialization class 'RKNSJSONSerialization' detected: Registering for MIME Type 'application/json 
2012-12-05 17:08:15.118 AppName Alpha[1310:1603] I restkit.network:RKHTTPRequestOperation.m:141 GET 'http://test1.AppName.com/albums/display/102/drrrbSAt6RFLsgHaKiiB6kBIyYRyzxLYinL84Kba' 
2012-12-05 17:08:15.122 AppName Alpha[1310:907] error (null), (null) 
2012-12-05 17:08:35.333 AppName Alpha[1310:3e13] I restkit.network:RKHTTPRequestOperation.m:158 GET 'http://test1.AppName.com/albums/display/102/drrrbSAt6RFLsgHaKiiB6kBIyYRyzxLYinL84Kba' (200) 
2012-12-05 17:08:35.351 AppName Alpha[1310:1507] CoreData: error: Failed to call designated initializer on NSManagedObject class 'AppName Album' 
2012-12-05 17:08:35.355 AppName Alpha[1310:1507] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<AppName 0x1c579380> valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "Title".' 
*** First throw call stack: 
(0x39e503e7 0x38ea9963 0x39e500d5 0x371f8d81 0x3276cc1d 0xbf545 0xc00f5 0xc0ab9 0xc44c5 0x32791299 0xb738b 0xb6e5f 0xb7ca5 0xb8343 0xb895b 0x32791299 0xf96ef 0xf8c4b 0x32791299 0x328097f7 0x32c38793 0x32c3c657 0x32c3c7d9 0x3312a7f1 0x3312a684) 
libc++abi.dylib: terminate called throwing an exception 

你可以在返回的JSON對象這裏峯: http://test1.AppName.com/albums/display/102/drrrbSAt6RFLsgHaKiiB6kBIyYRyzxLYinL84Kba

回答

46

這表明有人試圖創建一個對象,而無需調用相應的核心數據初始化,這可能意味着你得到一個RKObjectRequestOperation實例而不是RKManagedObjectRequestOperation

我懷疑響應描述符無法匹配URL,導致它選擇錯誤的對象請求操作類型。您可以通過將一個斷點appropriateObjectRequestOperationWithObject:method:path:parameters:在那讀線檢查:

NSArray *matchingDescriptors = RKFilteredArrayOfResponseDescriptorsMatchingPath(self.responseDescriptors, requestPath); 
BOOL containsEntityMapping = RKDoesArrayOfResponseDescriptorsContainEntityMapping(matchingDescriptors); 
BOOL isManagedObjectRequestOperation = (containsEntityMapping || [object isKindOfClass:[NSManagedObject class]]); 

這種邏輯是什麼是負責選擇操作創建的類型。檢查matchingDescriptors是否包含您期望的響應描述符,然後檢查下兩個布爾值的值。我的猜測是RKFilteredArrayOfResponseDescriptorsMatchingPath沒有回報你所期望的。

+0

這是什麼類?另外,第一個GET請求返回錯誤(null,null)。另外,爲什麼有兩個要求出去? :) –

+0

'appropriateObjectRequestOperationWithObject:method:path:parameters:'在RKObjectManager上。請注意,沒有2個請求出去。第一條日誌消息用於發送,第二條日誌消息包含響應中的(200)。這個'error(null,null)'消息來自你的失敗完成塊中的NSLog。 –

+0

我現在強制RKManagedObjectRequestOperation操作,它正在工作。我必須明天回去看看問題的根源是什麼。錯誤(null,null)不是來自我的。我甚至評論了該塊甚至要仔細檢查。 –

1

現在已經爲RKObjectManager讀取了類文檔,說明了基址的AF網絡處理的處理。如果我先讀過這篇文章,那麼我會爲自己節省一些時間,並且也會從關於RestKit示例中的路徑做出不相關的評論。

遇到問題匹配對象和實體映射的混合,包括許多對象映射與無關鍵路徑和不同的URL,並找到這個答案非常有用的確定問題的原因可能有助於其他人體驗類似的問題。

相關問題