Ios 删除restKit 0.20中的孤立对象
目前,我正在努力删除Ios 删除restKit 0.20中的孤立对象,ios,iphone,objective-c,restkit,rkobjectmapping,Ios,Iphone,Objective C,Restkit,Rkobjectmapping,目前,我正在努力删除孤立对象。 我有以下几节课 映射器类 在这个类中,我创建了所有RKObjectManager,并在其他类中使用它 -(RKObjectManager *)mapAppointments{ RKEntityMapping* appointmentMapping = [RKEntityMapping mappingForEntityForName:@"Appointment" inManagedObjectStore:managedObjectStore]; ap
孤立对象
。
我有以下几节课
映射器类
在这个类中,我创建了所有RKObjectManager,并在其他类中使用它
-(RKObjectManager *)mapAppointments{
RKEntityMapping* appointmentMapping = [RKEntityMapping mappingForEntityForName:@"Appointment" inManagedObjectStore:managedObjectStore];
appointmentMapping.identificationAttributes = @[@"app_id",@"app_start"] ;
[appointmentMapping addAttributeMappingsFromDictionary:@{
@"AddressInfo": @"app_addressinfo",
@"Completed": @"app_completed",
@"Description": @"app_description",
@"EndDate":@"app_end",
@"FullDay": @"app_fullday",
@"Id":@"app_id",
@"Label": @"app_label",
@"LabelId": @"app_label_id",
@"Location": @"app_location",
@"Private":@"app_private",
@"ProjectName":@"app_project_name",
@"ProjectNumber": @"app_project_number",
@"RecurrenceInfo": @"app_recurrenceInfo",
@"RelationAddressCity": @"app_relation_address_city",
@"RelationAddressId":@"app_relation_address_id",
@"RelationAddressName": @"app_relation_address_name",
@"RelationAddressStreet":@"app_relation_address_street",
@"RelationCode": @"app_relation_code",
@"RelationContactPersonId": @"app_relation_contact_id",
@"RelationContactPersonName": @"app_relation_contact_name",
@"RelationName":@"app_relation_name",
@"ReminderInfo":@"app_reminder_info",
@"StartDate": @"app_start",
@"State": @"app_state",
@"Subject": @"app_subject",
@"SupplierCode":@"app_supplier_code",
@"SupplierContactPersonId": @"app_supplier_contact_person_id",
@"SupplierContactPersonName":@"app_supplier_contact_person_name",
@"SupplierName": @"app_supplier_name",
@"Type": @"app_type",
@"ResxPers":@"app_resxPers",
}];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:appointmentMapping pathPattern:nil keyPath:@"" statusCodes:[NSIndexSet indexSetWithIndex:200]];
NSArray *arrResponsDescriptor = [[NSArray alloc]initWithObjects:responseDescriptor,nil];
[objectManager addResponseDescriptorsFromArray:arrResponsDescriptor];
return objectManager;
}
WEBSERVICE类
在这个类中,我执行请求并尝试删除孤立对象
-(void)fetchAppointmentsOnCompletionFor:(NSDate *)start andEnd:(NSDate *)end OnCompletion:(myCompletion) compblock{
Mapper *mapper = [Mapper new];
RKManagedObjectStore *store = [[AdsolutDataModel sharedDataModel] objectStore];
NSLog(@"store is %@",store);
NSManagedObjectContext *context = store.mainQueueManagedObjectContext;
RKObjectManager *objectManager = [mapper mapAppointments];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSString *strStart = [dateFormatter stringFromDate:start];
NSString *strEnd = [dateFormatter stringFromDate:end];
NSString *userName = [[NSUserDefaults standardUserDefaults]valueForKey:@"userName"];
NSString *hash = [[NSUserDefaults standardUserDefaults]valueForKey:@"hash"];
NSString *urlString = [NSString stringWithFormat:@"getcrmappointments?gebrcode=%@&token=%@&startdate=%@&enddate=%@",userName,hash,strStart,strEnd];
[objectManager addFetchRequestBlock:^NSFetchRequest *(NSURL *URL) {
RKPathMatcher *pathMatcher = [RKPathMatcher pathMatcherWithPattern:urlString];
NSDictionary *argsDict = nil;
BOOL match = [pathMatcher matchesPath:[URL relativePath] tokenizeQueryStrings:NO parsedArguments:&argsDict];
if (match) {
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Appointment"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"(app_start >= %@) AND (app_end <= %@)", start,end]; // NOTE: Coerced from string to number
fetchRequest.sortDescriptors = @[ [NSSortDescriptor sortDescriptorWithKey:@"app_id" ascending:YES] ];
return fetchRequest;
}
return nil;
}];
NSURLRequest *request = [objectManager requestWithObject:nil method:RKRequestMethodGET path:urlString parameters:nil];
RKManagedObjectRequestOperation *operation = [objectManager managedObjectRequestOperationWithRequest:request managedObjectContext:context success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"REQUEST URL = %@",request.URL);
NSError *error = nil;
BOOL success = [context save:&error];
if (!success) RKLogWarning(@"Failed saving managed object context: %@", error);
NSLog(@"SUCCESS");
NSError *saveError = nil;
for (Appointment *appointment in mappingResult.array) {
NSLog(@"Appointment title is %@",appointment.app_subject);
appointment.synchronized = @1;
appointment.app_delete = @0;
[context saveToPersistentStore:&saveError];
}
[self fetchAfwezighedenOnCompletionFor:start andEnd:end OnCompletion:^(BOOL finished) {
if(finished){
compblock(YES);
}
}];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
NSLog(@"Hit error: %@", error);
}];
[objectManager enqueueObjectRequestOperation:operation];
}
-(void)fetchAppointsonCompletionfor:(NSDate*)开始和结束:(NSDate*)结束完成:(myCompletion)compblock{
映射器*映射器=[Mapper new];
RKManagedObjectStore*store=[[AdsoluteDataModel sharedDataModel]objectStore];
NSLog(@“存储为%@”,存储);
NSManagedObjectContext*context=store.mainQueueManagedObjectContext;
RKObjectManager*objectManager=[mapper MapAppoints];
NSDateFormatter*dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
[日期格式化程序setDateFormat:@“dd-MM-yyyy”];
NSString*strStart=[dateFormatter stringFromDate:start];
NSString*strEnd=[dateFormatter stringFromDate:end];
NSString*userName=[[NSUserDefaults standardUserDefaults]valueForKey:@“userName”];
NSString*hash=[[NSUserDefaults standardUserDefaults]valueForKey:@“hash”];
NSString*urlString=[NSString stringWithFormat:@“GetCrMapPoints?gebrcode=%@&token=%@&startdate=%@&enddate=%@”,用户名、哈希、strStart、strengd];
[objectManager addFetchRequestBlock:^NSFetchRequest*(NSURL*URL){
RKPathMatcher*pathMatcher=[RKPathMatcher pathMatcherWithPattern:urlString];
NSDictionary*argsDict=nil;
BOOL match=[pathMatcher matchesPath:[URL relativePath]tokenizeQueryStrings:NO parsedArguments:&argsDict];
如果(匹配){
NSFetchRequest*fetchRequest=[NSFetchRequest fetchRequestWithEntityName:@“约会”];
fetchRequest.predicate=[NSPredicate predicateWithFormat:@”(应用程序启动>=%@)和(app_end查看RKObjectManager managedObjectRequestOperationWithRequest:
的代码表明,获取请求块是从传递给操作的对象管理器传递给操作的。因此,需要进行调试以找出为什么不调用它。在所有“成功”场景中都应该调用它(即响应描述符与接收到的响应匹配的响应)
您可以考虑使用管理器发出请求,而不是获取请求,也可以使用RKManagedObjectRequestOperation
,但从理论上讲,这应该只是编写更少的代码,而不是功能上的差异
尝试更改提取请求块:
RKPathMatcher *pathMatcher = [RKPathMatcher pathMatcherWithPattern:@"getcrmappointments"];
记录了什么?fetch块是否运行?如果运行,模式是否匹配?为什么显式使用RKManagedObjectRequestOperation
,而不是让管理器发出请求?@Wain对延迟响应表示抱歉。但是fetch块似乎没有运行。你知道这是怎么回事吗?似乎我进入了取回请求块。但他没有找到匹配项。我正在搜索这段时间,但似乎没有让它工作。有什么想法吗?