如何使用Restkit 0.20.0创build/发布新的托pipe对象到服务器?
我很难find创build新托pipe对象的文档或示例,设置它的值,并使用Restkit保存到服务器。
我有一个NSManagedObject Post:
@interface Post : NSManagedObject @property (nonatomic, retain) NSNumber * postID; @property (nonatomic, retain) NSString * title; @property (nonatomic, retain) NSString * text; @end
这是我的AppDelegate安装程序:
// ---- BEGIN RestKit setup ----- RKLogConfigureByName("RestKit/Network", RKLogLevelTrace); NSError *error = nil; NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"My_App" ofType:@"momd"]]; // NOTE: Due to an iOS 5 bug, the managed object model returned is immutable. NSManagedObjectModel *managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy]; RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel]; // Enable Activity Indicator Spinner [AFNetworkActivityIndicatorManager sharedManager].enabled = YES; // Initialize the Core Data stack [managedObjectStore createPersistentStoreCoordinator]; NSPersistentStore __unused *persistentStore = [managedObjectStore addInMemoryPersistentStore:&error]; NSAssert(persistentStore, @"Failed to add persistent store: %@", error); [managedObjectStore createManagedObjectContexts]; // Configure a managed object cache to ensure we do not create duplicate objects managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext]; // Set the default store shared instance [RKManagedObjectStore setDefaultStore:managedObjectStore]; // Configure the object manager RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://localhost:3000/api/v1"]]; objectManager.managedObjectStore = managedObjectStore; NSString *auth_token = [[LUKeychainAccess standardKeychainAccess] stringForKey:@"auth_token"]; // Getting the Auth_Token from keychain [objectManager.HTTPClient setAuthorizationHeaderWithToken:auth_token]; [RKObjectManager setSharedManager:objectManager]; // Setup Post entity mappping RKEntityMapping *postMapping = [RKEntityMapping mappingForEntityForName:@"Post" inManagedObjectStore:managedObjectStore]; [postMapping addAttributeMappingsFromDictionary:@{ @"title": @"title", @"text": @"text", @"id": @"postID"}]; RKResponseDescriptor *postResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:postMapping pathPattern:nil keyPath:@"post" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; [objectManager addResponseDescriptor:postResponseDescriptor];
现在,在我的NewPostViewController,当我点击我的“保存”button,我在我的导航栏,我需要做什么来保存这篇文章到我的服务器?
这是我所尝试的,但它不能正常工作。 我进入成功块,我的服务器得到了POST,但字段是零:
- (void)savePost { RKManagedObjectStore *objectStore = [[RKObjectManager sharedManager] managedObjectStore]; Post *post = [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:objectStore.mainQueueManagedObjectContext]; [post setTitle:@"The Title"]; [post setText:@"The Text"]; [[RKObjectManager sharedManager] postObject:post path:@"posts" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { NSLog(@"Success saving post"); } failure:^(RKObjectRequestOperation *operation, NSError *error) { NSLog(@"Failure saving post: %@", error.localizedDescription); }]; }
看起来你没有添加任何RKRequestDescriptor到你的对象pipe理器。 没有他们,可怜的对象pipe理器不能使用键/值魔法序列化您的对象到一个NSDictionary。
你已经添加的东西,RKResponseDescriptor,描述了如何pipe理响应。 这就是为什么你会看到所谓的成功块:RestKit不知道你要发送什么,但是它识别服务器响应中的Post对象。
尝试添加下面的responseDescriptor代码:
RKRequestDescriptor * requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[postMapping inverseMapping] objectClass:[Post class] rootKeyPath:@"post"]; [objectManager addRequestDescriptor:requestDescriptor];
(仔细检查keypath;我不知道你的API期望什么,所以我用你在响应描述符中的内容)
- 从AppBundle与CoreData加载图像
- 从设备上删除应用程序后,无法创buildNIL模型的NSPersistentStoreCoordinator
- CoreData:警告:无法为实体“Readings”加载名为“WEEReadings”的类。 找不到类,使用默认的NSManagedObject来代替
- CoreData有条件地使用NSPredicate(swift)获取NSDate
- 在保持NSFetchedResultControllerfunction的同时保存单个CoreData实体(不是整个上下文)
- 核心数据组按月和总和
- MR_SHORTHAND不起作用
- 如何自动生成NSManagedObject子类与date而不是NSDatedate属性?
- 麻烦与ViewControllers之间的segue