1

当设备连接到 WiFi 时,我的应用程序需要从 .Net WCF 服务获取数据。如果服务器上添加了新行,则应将其添加到其 CoreData 数据库中。我正在使用 NSDictionary 将本地对象与远程对象进行比较。代码是:

-(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。我已经注释了(根据我)应该将新对象中的值保存到托管对象的代码。有人可以帮我实现这一目标吗?

谢谢

4

1 回答 1

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

如果您有更多关于提取或分配有用数据(错误/警告/日志消息)时发生的情况的信息。

于 2012-09-26T05:50:13.893 回答