0

我正在尝试通过 RestKit 发布到服务器,并且在将帖子添加到 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];

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

4

0 回答 0