Это мой первый проект с использованием RestKit, и я пытаюсь синхронизировать свое простое приложение iOS с набором API, обслуживаемым приложением Django, используя пакет Tastypie. Мое приложение для iOS основано на приложении Core Data Default Xcode.
Я пытаюсь получить объекты из простой модели:
Автомобиль (имя (строка), общедоступное (логическое значение), идентификатор (целое число), создано (дата-время))
Мой API выглядит так для коллекций:
http://www.example.com/api/v1/car/?format=json
{"meta": {"limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 1}, "objects": [{"created": "2012-07-04T13:47:37+00:00", "id": "1", "name": "BMW", "public": true, "resource_uri": "\/api\/v1\/car\/1\/",}]}
И для 1 объекта:
http://www.example.com/api/v1/car/1/?format=json
{"created": "2012-07-04T13:47:37+00:00", "id": "1", "name": "BMW", "public": true, "resource_uri": "\/api\/v1\/car\/1\/",}
Учитывая этот контекст, в моем приложении iOS я настроил свой магазин в своем делегате приложения.
-(void)configureObjectStore {
// Initialize the RestKit Object Manager
RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURL:[[NSURL alloc] initWithString:@"http://www.example.com/api/v1"]];
objectManager.client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;
// Initialize object store
objectManager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"database.sqlite"];
[self coreDataMapping];
}
И установите мое сопоставление внутри Application Delegate (не уверен, что это должно быть здесь...)
- (void)coreDataMapping {
// Now for the object mappings
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Car" inManagedObjectContext:self.managedObjectContext];
RKManagedObjectMapping* carMapping = [RKManagedObjectMapping mappingForEntity:entity inManagedObjectStore:[RKObjectManager sharedManager].objectStore];
[carMapping mapKeyPath:@"id" toAttribute:@"carId"];
[carMapping mapAttributes:@"name", nil];
[carMapping mapKeyPath:@"public" toAttribute:@"public"];
[carMapping mapKeyPath:@"created" toAttribute:@"created"];
carMapping.primaryKeyAttribute = @"carId";
[[RKObjectManager sharedManager].mappingProvider addObjectMapping:carMapping];
}
Затем в моем основном контроллере, где я хочу загрузить в виде таблицы все свои объекты (все автомобили)
- (void)loadData {
// Load the object model via RestKit
RKObjectManager* objectManager = [RKObjectManager sharedManager];
[objectManager loadObjectsAtResourcePath:@"/car/1/?format=json" usingBlock:^(RKObjectLoader *loader) {
loader.objectMapping = [objectManager.mappingProvider objectMappingForClass:[Car class]];
loader.delegate = self;
}];
}
Когда я запускаю свое приложение и пытаюсь загрузить список объектов, оно запускает метод didFailWithError из делегата, и у меня есть следующий стек:
I restkit.core_data:RKInMemoryManagedObjectCache.m:34 Creating thread-local entity cache for managed object context: <NSManagedObjectContext: 0x887f1b0>
I restkit.core_data:RKInMemoryManagedObjectCache.m:50 Caching instances of Entity 'Car' by primary key attribute 'carId'
E restkit.core_data:RKManagedObjectStore.m:262 Core Data Save Error
NSLocalizedDescription: (null)
NSValidationErrorKey: (null)
NSValidationErrorPredicate: (null)
NSValidationErrorObject: (null)
E restkit.core_data:RKManagedObjectLoader.m:167 Failed to save managed object context after mapping completed: The operation couldn’t be completed. (Cocoa error 134020.)
Hit error: Error Domain=NSCocoaErrorDomain Code=134020 "The operation couldn’t be completed. (Cocoa error 134020.)" UserInfo=0x89612c0 {NSAffectedObjectsErrorKey=<Car: 0x6c85670> (entity: Car; id: 0x6c65790 <x-coredata:///Car/t318CE65F-E4FE-44E1-813B-GEZG54> ; data: {
created = "2012-07-04 13:47:37 +0000";
favorites = (); (Note this relation is not mapped in the API but it is not required)
carId = 1;
modified = nil;
name = BMW;
public = 1;
}), NSUnderlyingException=Store <NSSQLCore: 0x88848c0> (URL: file://localhost/Users/me/Library/Application%20Support/Documents/database.sqlite) cannot hold instances of entity (<NSEntityDescription: 0x886f650>) name Car, managedObjectClassName Car, renamingIdentifier Car, isAbstract 0, superentity name (null), properties {
created = "(<NSAttributeDescription: 0x886dce0>), name created, isOptional 1, isTransient 0, entity Car, renamingIdentifier created, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 900 , attributeValueClassName NSDate, defaultValue (null)";
favorites = "(<NSRelationshipDescription: 0x886e900>), name favorites, isOptional 1, isTransient 0, entity Car, renamingIdentifier favorites, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, destination entity Favorite, inverseRelationship list, minCount 0, maxCount 0, isOrdered 0, deleteRule 1";
carId = "(<NSAttributeDescription: 0x886dd80>), name carId, isOptional 1, isTransient 0, entity Car, renamingIdentifier carId, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 100 , attributeValueClassName NSNumber, defaultValue 0";
name = "(<NSAttributeDescription: 0x886de20>), name name, isOptional 1, isTransient 0, entity Car, renamingIdentifier name, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 700 , attributeValueClassName NSString, defaultValue (null)";
public = "(<NSAttributeDescription: 0x886de70>), name public, isOptional 1, isTransient 0, entity Car, renamingIdentifier public, validation predicates (\n), warnings (\n), versionHashModifier (null)\n userInfo {\n}, attributeType 800 , attributeValueClassName NSNumber, defaultValue (null)";
}, subentities (null), userInfo {
}, versionHashModifier (null)}
Я пробовал много вещей, но у меня такое чувство, что я хожу по кругу здесь. Любые советы будут тепло приветствоваться.
Спасибо за ваше время и поддержку, С уважением,
PS: Мы должны исключить ошибки, не связанные с RestKit, так как до интеграции приложение работало гладко.