2012-09-26 46 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