user1550951
user1550951

Reputation: 379

assign value from NSDictionary to NSManagedObject

My app requires to get data from a .Net WCF service when the device is connected to WiFi.If there's a new row added on the server,it should add it to its CoreData database. I am using a NSDictionary for comparing the local objects with the remote objects. The code is:

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

The problem is,I am not able to extract values from the remote object and assign it to the NSManagedObject.I have commented the code which (according to me) should save the values in new object to the managed object. Can someone please help me achieve this?

Thanks

Upvotes: 1

Views: 766

Answers (1)

Kibitz503
Kibitz503

Reputation: 867

Here is an example of a save I did in a recent project. I have somethings in wrappers so fetching a managed object and saving look a little weird on my end. Really the only major difference I see is the act of saving. Are you saving the new NSManagedObject elsewhere in the code?

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

If not it should looks something like this...

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

If you have anymore information about what's happening when you extract or assigning data that would be helpful (error/warning/log messages).

Upvotes: 1

Related Questions