Reputation: 331
maybe someone could tell me if i am on the right way.
I have two entities:
Exercise with following attributes: title - string ; exercise_id - int
AddedExercise with following attributes: count - int ; added_exercise_id - int
The AddedExercise has a fetched property called link_exercise with Destination: Exercise and predicate: exercise_id == $FETCH_SOURCE.added_exercise_id
In my code i do the following:
for(AddedExercise *e in listOfAddedEx){
[moc refreshObject:e mergeChanges:YES];
NSLog(@"%@", e.link_exercise);
}
And the log says:
Relationship fault for (), name link_exercise, isOptional 1, isTransient 1, entity AddedExercise, renamingIdentifier link_exercise, validation predicates ( ), warnings ( ), versionHashModifier (null) userInfo { }, fetchRequest (entity: Exercise; predicate: (exercise_id == $FETCH_SOURCE.added_exercise_id); sortDescriptors: ((null)); type: NSManagedObjectResultType; ) on 0x1700d8b80
So it seems like something is there, represented in a fault. But if i try to access the "link_exercise" array. For example with: e.link_exercise.count or e.link_exercise.lastObject
I'll get the following error:
'NSUnknownKeyException', reason: '[<_NSCoreDataTaggedObjectID 0xd000000000040002> valueForUndefinedKey:]: this class is not key value coding-compliant for the key added_exercise_id.'
Maybe someone has an idea how to solve that.
Many thanks in advance.
S.R.
-----> UPDATE 1:
I changed now the predicate to: SELF.exercise_id=exercise_id
Now i can access the Exercise object but i get a wrong id, because
SELF.exercise_id=exercise_id should look like SELF.exercise_id=added_exercise_id
With predicate SELF.exercise_id=added_exercise_id i get the following error:
'NSInvalidArgumentException', reason: 'Unable to generate SQL for predicate (exercise_id == added_exercise_id) (problem on RHS)'
-----> UPDATE 2:
Still cannot figure out why it isn't working...
For me it seems like
SELF.exercise_id == $FETCH_SOURCE.added_exercise_id
should be the right predicate, but it doesn't work. Maybe someone else has some suggestions...?
Or should i create a fetch request by myself (programmatically) ? That means set up a fetch request to all "Exercise"s and give the one back where exercise_id = added_exercise_id. This is basically that, what i currently try to solve through a fetched property.
-----> UPDATE 3: Ok i nearly have it!! One thing i forgot to mention is, that i use a multi managedObjectContext core data stack (Including a worker-, main- master-context)
masterContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
masterContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy;
[masterContext setPersistentStoreCoordinator: coordinator];
mainContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
mainContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy;
mainContext.parentContext = masterContext;
workerContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
workerContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy;
workerContext.parentContext = mainContext;
All my request go to the worker-context (in this case the fetched proper doesn't work) BUT if all my requests go to the master-context the fetched property works as expected!!
So it seems like it only works with the managedObjectContext which has the persistent store.
Can somebody explain me, why this cannot work with an other managedObjectContext?
Upvotes: 2
Views: 597