dogwasstar
dogwasstar

Reputation: 881

Core Data updating records does an insert instead

Not sure if any one can help and yes I have gone through all the post of stack exchange and other places.

I have an app which sends data to the server when we pull down to refresh. At the time of refresh of data already exists in the core data it suppose to update the content but it seems to be be updating and creating a new entry at the same time. so when I go back to the table its got multiple entries for the same data.

here is the code that I am using

NSArray *fetchedDealDetails = [appDelegate getDealsInfoByID:testId];
if(fetchedDealDetails.count > 0)
{

    Deals *typeList = [fetchedDealDetails objectAtIndex:0];

    typeList.name = [arr_temp valueForKey:@"name"];

    typeList.id = [arr_temp valueForKey:@"id"];

    typeList.coupon_background_image = [arr_temp valueForKey:@"coupon_background_image"];

    typeList.use_online = [arr_temp valueForKey:@"use_online"];

    typeList.coupon_download_btn_image = [arr_temp valueForKey:@"coupon_download_btn_image"];

    typeList.deal_provider_id = [arr_temp valueForKey:@"deal_provider_id"];

    typeList.details = [arr_temp valueForKey:@"details"];

    typeList.feature_deal = [arr_temp valueForKey:@"feature_deal"];

    typeList.haveDeleted = [NSNumber numberWithInt:0];

    context save:nil];

}

Not really sure if there is anything wrong with this code. any help would be greatly appreciated

Upvotes: 0

Views: 63

Answers (1)

Jignesh Agola
Jignesh Agola

Reputation: 320

Well i am not sure what your fetchedDealDetails Array contains. When ever you wanna update core data object you should get that object first and then update it

for getting particular object to update

NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"id= '%@'",[arr_temp valueForKey:@"id"]]]; [fetchRequest setPredicate:predicate];

Deals *typeList = [[context executeFetchRequest:fetchRequest error:nil]lastObject];

now assign the updated value to objectForUpdate

typeList.name = [arr_temp valueForKey:@"name"];
typeList.coupon_background_image = [arr_temp valueForKey:@"coupon_background_image"];
typeList.use_online = [arr_temp valueForKey:@"use_online"];
typeList.coupon_download_btn_image = [arr_temp valueForKey:@"coupon_download_btn_image"];
typeList.deal_provider_id = [arr_temp valueForKey:@"deal_provider_id"];
typeList.details = [arr_temp valueForKey:@"details"];
typeList.feature_deal = [arr_temp valueForKey:@"feature_deal"];
typeList.haveDeleted = [NSNumber numberWithInt:0];
[context save:nil];

Upvotes: 3

Related Questions