I got problem with core data, I had a table that exactly look like this
User (relationship with) Rack (relationship with) RackItem
How should I insert new object to rackItem with existing item object in User and existing Rack object
I try fetch the existing object
predicate =
[NSPredicate predicateWithFormat:@"userID = %@ AND rack.rackID = %@",
_userID,
@"1"];
entityName = @"RackItem";
NSArray *result = [self fetchDataWithEntity:entityName predicate:predicate];
id object = [result last object];
then on the setRackItem part I create a new EntityDescription and save it to core data using this function
- (id)objectInManagedObjectContextForDictionary:(NSDictionary *)dictionary
entity:(NSString *)entityName
managedObject:(id)object
insertKey:(NSArray *)keys
relation:(BOOL)isRelation {
// Recursive method
for (id key in dictionary) {
id value = [dictionary objectForKey:key];
NSString *camelCase = [key stringByReplacingCharactersInRange:NSMakeRange(0,1)
withString:[[key substringToIndex:1] uppercaseString]];
if ([value isKindOfClass:[NSDictionary class]] && isRelation) {
if ([keys containsObject:camelCase] && keys != nil) {
id newObject =
[NSEntityDescription insertNewObjectForEntityForName:camelCase
inManagedObjectContext:_managedObjectContext];
value = [self objectInManagedObjectContextForDictionary:[dictionary objectForKey:key]
entity:camelCase
managedObject:newObject
insertKey:keys
relation:isRelation];
} else {
SEL selector = NSSelectorFromString(key);
id newObject = [object performSelector:selector];
value = [self objectInManagedObjectContextForDictionary:[dictionary objectForKey:key]
entity:camelCase
managedObject:newObject
insertKey:keys
relation:isRelation];
}
}
NSString *methodName = [NSString stringWithFormat:@"set%@:", camelCase];
SEL selector = NSSelectorFromString(methodName);
[object performSelector:selector withObject:value];
}
return object;
}
And the result is good, data was inserted successfully. But the existing object got modified.
I think this is the problem of
id object = [result lastObject];
need to be retain. Anyone can help me?
Some Update
The result after followed insert
Before Insert
1|9|2|0|0||||248|52|||1|||||||
2|9|2|0|0|||||||||||||||
3|9|2|0|0|||||||||||||||
4|9|2|0|0|||||||||||||||
5|9|1|0|0|1||||||||||||||
sqlite> select *from zrackitem;
After Insert
1|9|2|0|0||||248|52|||1|||||||
2|9|2|0|0|||||||||||||||
3|9|2|0|0|||||||||||||||
4|9|2|0|0|||||||||||||||
5|9|2|0|0|||||||||||||||
6|9|1|0|0|1||||||||||||||
sqlite> select *from zrackitem;
it shows that every time I insert the existing row of zrack variable is gone.
This seems a little confusing …
first of all you should have of course as NeverBe said a to-many relationship between Rack and RackItem
Then it seems to me that you’re having a conceptual issue here.
If you want to insert a new object related to the same Rack than a previous RackItem … just fetch that Rack Object and insert your new RackItem by setting it’s rack property to the Rack you just fetched.
You really should keep it simple or you’re just gonna have a lot of Coredata inconstancy issues
Hope that this will help