我很难找到创建新托管对象、设置其值以及使用 Restkit 保存到服务器的文档或示例。
我有一个 NSManagedObject 帖子:
@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 中,当我单击导航栏中的“保存”按钮时,我需要做什么才能将此帖子保存到我的服务器?
这是我尝试过的,但它无法正常工作。我输入了成功块,我的服务器收到了 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);
}];
}