简体   繁体   English

RestKit与Django-Tastypie的集成

[英]Integration of RestKit with Django-Tastypie

This is my first project using RestKit and I am trying to make my simple iOS application synchronize with a set of API served by a Django application using the Tastypie package. 这是我的第一个使用RestKit的项目,我正在尝试使我的简单iOS应用程序与使用Deliciouspie软件包的Django应用程序提供的一组API同步。 My iOS application is based on the Core Data Default Xcode application. 我的iOS应用程序基于Core Data Default Xcode应用程序。

I'm trying to retrieve objects from a simple model : 我正在尝试从一个简单的模型中检索对象:

Car (name (string), public (boolean), id (integer), created (datetime)) 汽车(名称(字符串),公共(布尔值),ID(整数),创建的(日期时间))

My API looks like that for collections : 我的API类似于集合:

http://www.example.com/api/v1/car/?format=json 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\/",}]}

And for 1 object : 对于1个对象:

http://www.example.com/api/v1/car/1/?format=json 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\/",}

Given this context, in my iOS application I have configured my store in my Application Delegate 在这种情况下,我已在iOS应用程序中的Application Delegate中配置了商店

-(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];
}

And set my mapping inside by Application Delegate (not sure that this should be in here...) 并通过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];
}

Then in my main controller, where i want to load in a table view all of my objects (all of the cars) 然后在我的主控制器中,我要在表格中加载所有对象(所有汽车)的视图

- (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;
}];
}

When i fire my app and try to load the objects list, it triggers the didFailWithError method from the delegate and i have the following stack : 当我启动我的应用程序并尝试加载对象列表时,它会从委托中触发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)}

I have tried out many things but i have the feeling that i'm going round in circles here. 我已经尝试了很多东西,但是我有种在这里转转的感觉。 Any advice will be warmly welcome. 任何建议都将受到热烈欢迎。

Thank you for your time and support, Regards, 感谢您的时间和支持,问候,

PS : We should be able to rule out errors unrelated to RestKit since the application was working smoothly before the integration. PS:由于集成之前应用程序运行正常,因此我们应该能够排除与RestKit无关的错误。

Since this question was left without answer, I'll answer it myself with how I went forward with these issues. 由于这个问题没有答案,我将自己回答这些问题。

I have actually abandoned RestKit which is certainly very powerful but I couldn't bent it enough so it would work with Django-Tastypie instead I went to AFNetworking which is less high-level but allowed me to do my own API to Core Data backend and turned out quite convenient. 实际上,我已经放弃了RestKit,它当然非常强大,但是我不能足够弯曲,因此它可以与Django-Tastypie一起使用,而我去了不那么高级的AFNetworking ,但允许我为Core Data后端做自己的API,原来很方便。

I hope this might help some other lost soul. 我希望这可以帮助其他失落的灵魂。 Regards, 问候,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM