2014-01-27 114 views
2

孤立的對象此刻的我與Orphaned objects刪除掙扎。 我有以下類。刪除在restKit 0.20

映射類

在這個類中創建我的所有RKObjectManagers和我在其他類中使用它。

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

問題

目前,該Orphaned objects不會被刪除。我是在做一些錯誤的事情還是沒有正確設置?

感謝您的幫助!

+0

什麼記錄?獲取塊是否運行?如果運行,模式匹配是什麼?你爲什麼要明確使用'RKManagedObjectRequestOperation'而不必經理髮出請求? – Wain

+0

爲應對後期@Wain遺憾。但看起來,獲取塊沒有運行。安妮的想法怎麼可能? – Steaphann

回答

1

看爲RKObjectManager managedObjectRequestOperationWithRequest:代碼示出了讀取請求塊從對象管理器通過傳遞給操作。因此,需要調試才能確定它未被調用的原因。它應該在所有'成功'場景中被調用(即響應描述符與接收到的響應相匹配的場景)。

你可以看看使用管理器來發出請求,而不是獲取請求並使用RKManagedObjectRequestOperation,但理論上這應該只是編寫的代碼而不是功能差異。


嘗試改變讀取請求塊:

RKPathMatcher *pathMatcher = [RKPathMatcher pathMatcherWithPattern:@"getcrmappointments"]; 
+0

看來我進入了獲取請求塊。但他沒有找到一個匹配。我正在尋找這一段時間,但似乎並沒有得到它的工作。也許有任何想法? – Steaphann