如何通过RestKit 0.20发布到数据库

时间:2013-06-21 21:47:26

标签: ios json restkit afnetworking

我正在尝试使用restkit POST到API。 当我按保存后 - 我收到一个错误,显示一个空帖子并提示说:

Expected status code in (200-299), got 422" UserInfo=0xb83ca20 {NSLocalizedRecoverySuggestion={"body":["can't be blank"]}, 

帖子显示在tableView中,但我输入的文本没有显示在数据库中。 任何想法为什么会这样?我见过的常见问题是因为人们缺少将数据解析为json或因为缺少responseDescriptor - 但我有这个并且我没有得到json错误。它只是没有保留我输入的文字。 这是为什么?

我的savePost方法就像这样

RKManagedObjectStore *objectStore = [[RKObjectManager sharedManager] managedObjectStore];
Post *post = [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:objectStore.mainQueueManagedObjectContext];
[post setBody:self.postTextField.text];
[objectStore.mainQueueManagedObjectContext save:nil];

RKObjectManager *manager = [RKObjectManager sharedManager]; [manager postObject:self path:@"/posts.json" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NSLog(@"Success saving post");
   // [MUser setCurrentUser:self];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"Failure saving post: %@", error.localizedDescription);
}];

在我的appDelegate中,我有这样的设置:

 NSError *error = nil;
    NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Markofresh" 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];

    // 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:@"https://fierce.herokuapp.com"]];
    objectManager.managedObjectStore = managedObjectStore;
    [objectManager setRequestSerializationMIMEType:@"application/json"];
    [objectManager setAcceptHeaderWithMIMEType:@"application/json"];


    [RKObjectManager setSharedManager:objectManager];
    //setup Post Entity map
    RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"Post" inManagedObjectStore:managedObjectStore];
    [entityMapping addAttributeMappingsFromDictionary:@{
     @"id":             @"postID",
     @"url":            @"jsonURL",
     @"body":           @"body",
     @"created_at":     @"createdAt"}];
    entityMapping.identificationAttributes = @[ @"postID" ];

    [RKObjectManager sharedManager].requestSerializationMIMEType = RKMIMETypeJSON;

    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entityMapping pathPattern:@"/posts.json" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

    [objectManager addResponseDescriptor:responseDescriptor];

    RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[entityMapping inverseMapping] objectClass:[Post class] rootKeyPath:@"/posts.json"];
    [objectManager addRequestDescriptor:requestDescriptor];

我已经看到其他人对此感到困惑,并想知道AFNetworking是否可能是这种简单方法的更好选择 - 并且还有更多文档。 我知道我要做的事情很简单,而且我没有得到导致空帖的原因。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

你应该这样做:

[manager postObject:post path:@"/posts.json" parameters:nil  ...

所以你的问题只是一个错字?


尝试启用映射跟踪日志记录,以便您可以看到发生了什么:

RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);

考虑像Charles Proxy这样的东西来向你展示究竟是什么击中了电线。