ragu89
ragu89

Reputation: 345

POST with RestKit 0.20 - NSManagedObjectContext issue

I'm trying to make a POST request with RestKit, but when I call the postObject method of my RKObjectManager I get the following error :

Can only use -performBlockAndWait: on an NSManagedObjectContext that was created with a queue.

This error came when the method performBlockAndWait is called by the RKManagedObjectRequestOperation class.

Here is my code :

RKObjectMapping *responseMapping = [RKObjectMapping requestMapping];
[responseMapping addAttributeMappingsFromDictionary:@{
 // ...
 }];

NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
NSString *pathPattern = @"myPath";
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping method:RKRequestMethodPOST pathPattern:pathPattern keyPath:nil statusCodes:statusCodes];

RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:@{
 // ...
 }];

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[MyClass class] rootKeyPath:@"" method:RKRequestMethodPOST];

NSURL *baseURL = [NSURL URLWithString:@"http://192.168.1.1:8080"];
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:baseURL];
[manager addRequestDescriptor:requestDescriptor];
[manager addResponseDescriptor:responseDescriptor];

NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
manager.managedObjectStore = managedObjectStore;

[managedObjectStore createPersistentStoreCoordinator];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"mySqliteDatabaseFile"];
NSError *error;
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];

NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);

[managedObjectStore createManagedObjectContexts];
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];

// POST to create
@try {
    [manager postObject:visitReport path:@"myPath" parameters:nil success:^( RKObjectRequestOperation *operation , RKMappingResult *mappingResult ) {
        NSLog(@"Success");
    } failure:^( RKObjectRequestOperation *operation , NSError *error ){
        NSLog(@"Failure - %@",error);
    }];
}
@catch (NSException *exception) {
    NSLog(@"error - %@", exception);
}

I don't understand the problem with my context, any help is welcomed. Thank you.

Upvotes: 0

Views: 264

Answers (1)

ragu89
ragu89

Reputation: 345

Ok finally I find the problem :

The object visitReport I tried to POST is not from the NSManagedObjectContext expected by RestKit.

To solve, I create a new object :

MyObject *visitReportContext = [NSEntityDescription insertNewObjectForEntityForName:@"tableName" 
                                                    inManagedObjectContext:objectStore.mainQueueManagedObjectContext];
visitReportContext.Id = visitReport.Id
visitReportContext.title = visitReport.title
// etc..

Then, when I POST my new object, it works. But it's not very beautiful... How can I deal with my old object visitReport without create a new one in this "RestKit context" ?

Upvotes: 1

Related Questions