2012-09-26 44 views
1

當設備連接到WiFi時,我的應用程序需要從.Net WCF服務獲取數據。如果服務器上添加了新行,則應將其添加到其CoreData數據庫。我正在使用NSDictionary比較本地對象與遠程對象。該代碼是:將值從NSDictionary分配給NSManagedObject

-(void)handleGetAllCategories:(id)value 
{ 
    if([value isKindOfClass:[NSError class]]) 
    { 
     NSLog(@"This is an error %@",value); 
     return; 
    } 

    if([value isKindOfClass:[SoapFault class]]) 
    { 
     NSLog(@"this is a soap fault %@",value); 
     return; 
    } 

    NSMutableArray *result = (NSMutableArray*)value; 
    NSMutableArray *remoteObj = [[NSMutableArray alloc]init]; 

    for (int i = 0; i < [result count]; i++) 
    { 
     EDVCategory *catObj = [[EDVCategory alloc]init]; 
     catObj = [result objectAtIndex:i]; 
     [remoteObj addObject:catObj]; 
    } 

    NSArray *remoteIDs = [remoteObj valueForKey:@"categoryId"]; 

    NSFetchRequest *request = [[[NSFetchRequest alloc] init]autorelease]; 
    request.predicate = [NSPredicate predicateWithFormat:@"categoryId IN %@", remoteIDs]; 

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Categories" inManagedObjectContext:__managedObjectContext]; 
    [request setEntity:entity]; 
    NSMutableArray *results = [[NSMutableArray alloc]initWithArray:[__managedObjectContext executeFetchRequest:request error:nil]]; 

    NSArray *existingIDs = [results valueForKey:@"categoryId"]; 
    NSDictionary *existingObjects = [NSDictionary dictionaryWithObjects:results forKeys:existingIDs]; 

    for (NSDictionary *remoteObjectDic in remoteObj) 
    { 
     Categories *existingObject = [existingObjects objectForKey:[remoteObjectDic valueForKey:@"categoryId"]]; 

     if (existingObject) 
     { 
      NSLog(@"object exists"); 
     } 
     else 
     { 
      NSLog(@"create new local object"); 
//   Categories *newCategory; 
//   newCategory = [NSEntityDescription insertNewObjectForEntityForName:@"Categories" inManagedObjectContext:__managedObjectContext];   
//   [newCategory setCategoryId:[NSNumber numberWithInt:[[remoteObjectDic objectForKey:@"categoryId"]intValue]]]; 
//   [newCategory setCategoryName:[remoteObjectDic objectForKey:@"categoryName"]]; 
//   [newCategory setDocCount:[NSNumber numberWithInt:[[remoteObjectDic objectForKey:@"docCount"]intValue]]]; 
//   [newCategory setCategoryType:[NSNumber numberWithInt:[[remoteObjectDic objectForKey:@"categoryType"]intValue]]]; 
//   [newCategory setSubCategoryId:[NSNumber numberWithInt:[[remoteObjectDic objectForKey:@"subCategoryId"]intValue]]]; 
//   [__managedObjectContext insertObject:newCategory]; 
     } 
    } 
[my_table reloadData]; 
} 

的問題是,我不能夠從遠程對象中提取值,並將其分配給NSManagedObject.I有評論它(據我)的代碼應該保存在新的價值觀對象到託管對象。有人可以幫我實現嗎?

謝謝

回答

1

這是我在最近的項目中做了一個保存的例子。我有些東西在包裝器中,所以抓取一個託管對象,並在我的結尾保存看起來有點奇怪。真的,我看到的唯一的主要區別是儲蓄行爲。你是否在代碼的其他地方保存了新的NSManagedObject?

dict = (NSDictionary*)data; 
     @try { 
      if (dict) { 
       CaretakerInfo* info = [GenericDataService makeObjectWithEntityName:NSStringFromClass([CaretakerInfo class])]; 
       [info setName:[dict valueForKey:@"name"]]; 
       [info setImageURL:[dict valueForKey:@"photo"]]; 
       [info setCaretakerID:[dict valueForKey:@"id"]]; 
       [GenericDataService save]; 
      } 
      else { 
       theError = [Error createErrorMessage:@"No Data" Code:-42]; 
      } 
     } 
     @catch (NSException *exception) { 
      //return an error if an exception 
      theError = [Error createErrorMessage:@"Exception Thrown While Parsing" Code:-42]; 
     } 

如果不是應該看起來是這樣的......

 NSError *error = nil; 
    [context save:&error]; 

如果您對所發生的事情,當你提取或分配數據將是有益的(錯誤/警告/消息記錄了信息)。

+0

: - 謝謝,它的功能就像一個魅力! – user1550951

+0

現在,如果我要從CoreData中刪除一個對象(bcoz它在服務器上被刪除),我該如何做? – user1550951

+0

[context deleteObject:object];不要忘記保存後:) – Kibitz503