RestKit Post Failure管理对象存储区为零

时间:2013-06-20 18:36:38

标签: ios json heroku restkit afnetworking

我正在尝试通过RestKit POST到服务器,并且当帖子被添加到tableView时,它不会保存到服务器,我在xcode控制台日志中收到此错误

2013-06-20 11:08:44.522 App[9653:c07] W restkit:RKObjectManager.m:577 Asked to create an `RKManagedObjectRequestOperation` object, but managedObjectStore is nil.

但我相信我在appDelegate中设置了managedObjectStore。 我有这个:

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

    NSString *path = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"MyCoreDataName.sqlite"];
    NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:path fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];
    if (! persistentStore) {
        RKLogError(@"Failed adding persistent store at path '%@' : %@", path, error);
    }
   // NSAssert(persistentStore, @"Failed to add persistent store: %@", error);
    [managedObjectStore createManagedObjectContexts];
    //manager.managedObjectStore = managedObjectStore;

    // 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;

    [RKObjectManager setSharedManager:objectManager];

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

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://fierce.herokuapp.com/posts.json"]];
    RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[responseDescriptor]];
    operation.managedObjectContext = managedObjectStore.mainQueueManagedObjectContext;
    operation.managedObjectCache = managedObjectStore.managedObjectCache;
    [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
        Post *post = [result firstObject];
        NSLog(@"Mapped the post: %@", post);
    }failure:^(RKObjectRequestOperation *operation, NSError *error) {
        NSLog(@"Failed with error: %@", [error localizedDescription]);
    }];
    NSOperationQueue *operationQueue = [NSOperationQueue new];
    [operationQueue addOperation:operation];

我能够从服务器获取对象,当我POST时,对象出现在更新的提要中但不会保存到服务器。 我是否未正确设置managedObjectStore? 任何输入都会很棒。

0 个答案:

没有答案