RestKit在响应时创建NSManagedObject重复项

时间:2013-12-11 10:50:03

标签: objective-c post duplicates restkit

我正在向服务器发送一个Alarm对象,服务器会给我一个带有json的响应: “状态” - > “好” “闹钟” - > 我设置了identificationAttributes和managedObjectCache。但是Restkit仍然会创建一个重复的对象。还有什么我需要照顾的吗?

-(void)sendAlarm:(Alarm *)alarm{

RKManagedObjectStore *objectStore = [[RKManagedObjectStore alloc] initWithPersistentStoreCoordinator:((InsulyneAppDelegate*) [[UIApplication sharedApplication] delegate]).persistentStoreCoordinator];

[objectStore createManagedObjectContexts];
RKObjectMapping *alarmRequestMapping = [alarm alarmObjectMapping];
RKRequestDescriptor * requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:alarmRequestMapping objectClass:[Alarm class] rootKeyPath:@"alarm" method:RKRequestMethodPOST];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[AlarmResponse alarmResponseMappingInObjectStore:objectStore]  method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://www.xxx.com"]];
manager.managedObjectStore = objectStore;

[manager addRequestDescriptor:requestDescriptor];
[manager addResponseDescriptor:responseDescriptor];

NSManagedObjectContext *context = manager.managedObjectStore.mainQueueManagedObjectContext;

objectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:context];

NSMutableURLRequest* alarmRequest = [manager requestWithObject:alarm method:RKRequestMethodPOST path:@"/index.php" parameters:nil];




RKManagedObjectRequestOperation* operation = [manager managedObjectRequestOperationWithRequest:alarmRequest managedObjectContext:context success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
 NSLog(@"Success sending alarm");
    AlarmResponse *result = [mappingResult firstObject];
    NSLog(@"status is: %@, alarm time is: %@", [result status],[result.alarm alarmTime]);
 } failure:^(RKObjectRequestOperation *operation, NSError *error) {
 NSLog(@"Failure saving post: %@", error.localizedDescription);
 }];

 [[NSOperationQueue currentQueue] addOperation:operation];
}


+(RKObjectMapping *)alarmResponseMappingInObjectStore:(RKManagedObjectStore *)objectStore{
RKEntityMapping* alarmMapping = [Alarm alarmEntityMappingInManagedStore:objectStore];

RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[AlarmResponse class]];
[responseMapping addAttributeMappingsFromArray:@[@"status"]];
[responseMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"alarm" toKeyPath:@"alarm" withMapping:alarmMapping]];
return responseMapping;
}

+(RKEntityMapping *)alarmEntityMappingInManagedStore:(RKManagedObjectStore *)objectStore{
RKEntityMapping* alarmMapping = [RKEntityMapping mappingForEntityForName:@"Alarm" inManagedObjectStore:objectStore];
// NOTE: When your source and destination key paths are symmetrical, you can use addAttributesFromArray: as a shortcut instead of addAttributesFromDictionary:
[alarmMapping addAttributeMappingsFromArray:@[ @"authorEmail", @"recipientEmail", @"message", @"alarmTime" ]];
alarmMapping.identificationAttributes = @[@"alarmTime"];
return alarmMapping;
}

0 个答案:

没有答案