Core data:- How to update values in the array (Transformable)

1k Views Asked by At

I work on app that use Core data to save data in local device. In Core data i have save data in array using Transformable format but, i don't know how to update particular values in the array. My code for update Array is here

 NSManagedObjectContext *context = [self managedObjectContext];
   NSManagedObject *user = [NSEntityDescription    insertNewObjectForEntityForName:@"Type" inManagedObjectContext:context];
 NSError *error = nil;
 //Set up to get the thing you want to update
 NSFetchRequest * request = [[NSFetchRequest alloc] init];
   NSEntityDescription *entity = [NSEntityDescription entityForName:@"Type"inManagedObjectContext:context];
 [request setEntity:entity];
 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"businessTypes == %@", @"Others"];
 [request setPredicate:predicate];

   AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication]delegate];
  NSArray *results = [self.managedObjectContext executeFetchRequest:request error:&error];

if (results == nil) {
    // This implies an error has occurred.
    NSLog(@"Error from Core Data: %@", error);
} else {
    if (results.count == 0) {
        // No objects saved, create a new one...
    } else {
        // At least one object saved.  There should be only one
        // so use the first...
        user = [results lastObject];
        [user setValue:@"Management" forKey:@"businessTypes"];
    }
}

if (![self.managedObjectContext save:&error]) {
   //Handle any error with the saving of the context
}
else{
  [app saveContext];
   NSLog(@"update value successfully");
}

and below is my save array in core data:

{
    businessTypes =         (
        "Social Bussiness",
        Marketing,
        Transports,
        Others
    );
},

so i want to update "Others" to "Management" in the array.

When i run this code i have no error but i don't update particular value at index array. thanks to help me.

2

There are 2 best solutions below

0
abrar ul haq On BEST ANSWER

You have to modify your update function like this code then you will get your required output

NSManagedObjectContext *context = [self managedObjectContext];
NSError *error = nil;
NSFetchRequest * request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Type"inManagedObjectContext:context];
[request setEntity:entity];
 request.propertiesToFetch= @[ @"businessTypes"];
AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication]delegate];
NSArray *results = [self.managedObjectContext executeFetchRequest:request error:&error];

if (results == nil) {
    // This implies an error has occurred.
    NSLog(@"Error from Core Data: %@", error);
} else {
    if (results.count == 0) {
        // No objects saved, create a new one...
    } else {


        int loopCount = (int)results.count;
        Type* entityType = nil;
        for (int index=0; index<loopCount; index++) {
            entityType = (Type*)results[index];
            if (entityType.businessTypes!=nil) {
                NSUInteger reqIndex = [entityType.businessTypes indexOfObject:@"Others"];
                [entityType.businessTypes replaceObjectAtIndex:reqIndex withObject:@"Management"];
                [entityType setValue:entityType.businessTypes forKey:@"businessTypes"];
            }

        }
}
if (![self.managedObjectContext save:&error]) {
    //Handle any error with the saving of the context
}
else{
    [app saveContext];
    NSLog(@"update value successfully");
}
1
Mundi On

Perhaps you are confusing your entities. You fetch an entity called Type but you are calling the object user, indicating that perhaps you wanted to fetch a user that has a certain business type.

If each user has only one "business type", you do not need a Type entity, just a string attribute for the User entity.

If each user can have more than one business type, you should have an entity Type with a name attribute that includes one term indicating the business type, and it should be modeled as a many-to-many relationship.

User <<--------->>  Type

To set all types that are now called "Other" to "Management", you would fetch the Type with name "Other", change it and save. To only change one of a user's business types from "Other" to "Management", you would: fetch the user, remove the "Other" type, fetch the "Management" type, add it to the user and save.

If your businessTypes attribute is supposed to be a transformable array with hard-coded strings, you should probably change the data model as described above. You will have much more flexibility and power for searching and handling the data with a clean Core Data model.