Core Data does not update inverse relationship - ios

My two entities are Book and Category. A book can have many categories, and a category can have many books. The relationships are marked as the inverse of each other in the data model.
When I update the categories of a book I expected that the books of the corresponding categories would be updated automatically. In other words:
Book *book = [Book insertNewObjectIntoContext:context];
Category *category = [Category insertNewObjectIntoContext:context];
[book addCategories:[NSSet setWithObject:category]];
BOOL result = [category.books containsObject:book]; // Should be YES
Yet, I get NO in my app. books returns an empty set.
I triple-checked that the relationships are marked as inverse. What else could be at play here? Or is there something I'm missing?
Would it hurt if I set the inverse relation manually? If I do everything works like a charm.

The inverse relationship is not updated until the end of the run loop. Calling it immediately after like that does not give it time to update.
If you were to do something like:
dispatch_async(dispatch_get_main_queue(), ^{
BOOL result = [category.books contains:book];
NSLog(#"Result: %#", result ? #"YES" : #"NO");
});
You would see your expected result.
Update
It does not hurt to set the reverse manually. When everything is working correctly it results in a waste of cycles because Core Data has to double check the relationships so you will see a performance hit.

You can call
[context processPendingChanges];
to force the managed object context to update the inverse relationships immediately.
This method is automatically called during the event loop, or when you save the
context. But you can call it explicitly as well. From the documentation:
Forces the receiver to process changes to the object graph.
...
You can also invoke it manually to coalesce any pending unprocessed
changes.

Related

Core Data applying delete rule when updating

Beginner in Core Data here, there is something very basic in Core Data that I don't understand. I know the delete rules, such that if my object gets deleted, if it has relationships that are cascade for example, those relationships will get deleted as well. But what happens on updates?
Example:
Person has a relationship to a car. Delete rule is cascade.
Person --> Car
If Person is deleted, Car will be gone too.
But now what if Person just points to another Car, the previous Car will NOT be deleted and will just be dangling in the DB.
Any solutions to this?
I figured ideally you should delete the first car before setting the new one, but this is automatically done from a server fetch.
If this is the behavior you want in all cases you could override the managed object subclass method to set the new relationship. In the method, check first if another object exists and delete it if desired.
E.g.
-(void) setCar:(Car *)car {
if (Car* oldCar = self.car) {
[self.managedObjectContext deleteObject:oldCar];
}
[self willChangeValueForKey:#"car"];
[self setPrimitiveValue:car forKey:#"car"];
[self didChangeValueForKey:#"car"];
}

NSManagedObject ignoring changed transformable

I have a very basic caching system for some objects that conform to NSCoding. A method takes the object, extracts some information for keys and search parameters, creates or updates an NSManagedObject, and saves it to the persistent store:
+ (void)updateQuestion:(StacManQuestion *)question site:(StacManSite *)site
{
// 1. Look up with a basic predicate.
SECachedQuestion *cachedQuestion = [SECachedQuestion
findFirstWithQuestionId:question.questionId site:site];
// 2. Create if missing.
if (!cachedQuestion) {
cachedQuestion = [SECachedQuestion MR_createEntity];
cachedQuestion.questionId = question.questionId;
cachedQuestion.site = site.apiSiteParameter;
}
// 3. Update properties.
cachedQuestion.isFavorite = question.favorited;
cachedQuestion.lastAccessTime = [NSDate date];
cachedQuestion.question = question;
// 4. Save
[[NSManagedObjectContext MR_contextForCurrentThread]
MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
/* ... */
}];
}
The first time this code runs, it works perfectly. The object gets saved to the cache and is instantly available for the next run. After that, however, assigning a new value to cachedQuestion.question does nothing.
I've added some breakpoints and found the following:
The assignment to question does not update changedValues:
(lldb) po [cachedQuestion changedValues] {
lastAccessTime = "2014-07-16 17:16:45 +0000";
}
question and cachedQuestion.question start out with the different addresses and then end with the same so an assignment is definitely occurring.
If I put a step before the assignment that sets cachedQuestion.question = nil, I get the following, but the the record for question disappears again on reassignment.
(lldb) po [cachedQuestion changedValues] {
lastAccessTime = "2014-07-16 17:16:45 +0000";
question = "<null>";
}
Here's another super up thing. I went ahead and did a test where I just set cachedQuestion.question = nil for a few builds of the app. This is working fine for subsequent lookups in a single run, but every time when the app launches the first read of cachedQuestion.question goes back to the original value! I can confirm that new cached questions are begin created and lastAccessTime has no problem updating. It's just this one field that is frozen in time. This was because of data validation. nil got it to show up in changed values but save failed as it was not optional.
UPDATE
Right now I'm doing a sad sad hack where any time there is a change I delete the old record and insert a new one. It works well enough but I'd much rather know what's going wrong.
You're not writing your changes to the correct context. First, stop using contextForCurrentThread. It's deprecated and will cause you a crash 1 time out of 100, which is one too many. You also risk crashing since this method can and will return a context that is not the default context. Making managed object assignments across contexts will also crash, for good reason.
Instead, you should explicitly create your context, write your changes to it, and then save it at the end of your method. That will likely solve the majority of the problems you're seeing.
I had to read this several times...
I think the problem here is that you do not use relationships correctly.
NSManagedObjects should be set up as an Entity.
It should not be Transformable but a relationship to an entity.
From the context you can get the first all objects in the first entity in a MutableArray, then get all of its related objects in a new MutableArray of objects through its relationship by calling this when one object is selected:
self.newArray = [[self.oldObject.relationshipName allObjects]mutableCopy];
You will then only get content related to selected object and not all objects in the related entity.
This means there should not be a question attribute but a relationship to the entity question.

unwanted objects appearing in core data relationship

Long question---thanks in advance for your time. After saving new managed objects, I am finding them added to a relationship on another object in my core data database---one for which my code calls no setter method and that has no inverse relationship. I have pored over the code and used logs to isolate the occurrence the best I can, but I'm encountering bizarre behavior I cannot explain (or fix).
More specifically:
I have an entity called PendingSyncTracker. It simply has one relationship, objectsToSync. I have not yet added any line in my code to call a setter method on this relationship. It is a to-many relationship. It points to BaseEntity. For the "Inverse" option, I have selected "No Inverse Relationship."
When I load a particular table view, 3 objects are downloaded from a server and then parsed into managed objects and saved. By the time the table view begins loading cells, 2 of those 3 objects will mystifyingly be present in the objectsToSync relationship.
I have used NSLog all over my code to figure out exactly when these objects can first be found as members of the objectsToSync set.
NSSet *objectsToSync = [[[SyncEngine sharedEngine] fetchClassNamed:#"PendingSyncTracker" withPredicates:nil][0] valueForKey:#"objectsPendingSync"];
NSLog(#"PendingSyncTracker objectsToSync set (%lu objects): %#", (unsigned long)[objectsToSync count], objectsToSync);
The answer to when they first appear in the set actually varies depending on where I do/don't place those 2 lines of code!
The objects are never found on the relationship before the managed object context is saved in the course of saving my 3 new core data objects.
If I don't use those 2 lines till I'm back in the Table View Controller that sent the new objects off to the Sync Engine to be stored locally (where the MOC is accessed and saved), then the log will there reveal that 2 objects have been added to the relationship.
If I use those 2 lines immediately after saving the MOC in the Sync Engine, then the logs will indicate (both there and back in the TVC) that only 1 object has been added to the relationship.
If I use those 2 lines immediately before and after saving the MOC (and back in the TVC), then all 3 logs will reveal that the relationship contains an empty set.
I also have those 2 lines at the beginning of cellForRowAtIndexPath. Regardless of prior logs, that log will always indicate that 2 objects have been added to the relationship.
All 3 of the managed objects that are created in the Sync Engine are stored as entity types that are subEntities of BaseEntity (to which the objectsToSync relationship points). The 2 types that get added to the relationship are each defined to have a reciprocal relationship, but with a different object, not PendingSyncTracker (although the different object is a subEntity of BaseEntity!).
So.. what explains these observations? How are these objects getting added to the relationship?
UPDATE:
- (NSArray*) fetchClassNamed:(NSString*)className withPredicates:(id)parameters;
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:className inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// set predicates
if (!(parameters == nil)) {
[fetchRequest setPredicate:parameters];
}
NSError *error;
NSArray *fetchedResults = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
return fetchedResults;
}
First, what does [[[SyncEngine sharedEngine] fetchClassNamed... do? Just a guess but it is doing something with KVC to set the relationship for you.
Also, you should always, always, always have an inverse relationship. Even if you never use it, Core Data does. Not having an inverse can lead to lots of issues, including but not limited to performance problems and potentially data corruption.
Add an inverse relationship and update your question with what -fetchClassNamed... does.

Adding a relationship in core data

I have been at this single task for several days trying to get the relationships between core data entities working. I have achieved this but now I need to change it so that the new attribute value has its relationship added to an existing object. It is a 1 - to - many database.
I am not sure how to add a relationship to a object that already exists. So in the new object that is getting added to RoutineDetail, how would I create the relationship to the object that already exists in the routine Entity?
I have looked at several examples all showing how to add relationships to newly added objects but I need it so the new object in RoutinesDetails has a relationship with the value that already exists in Routines.
The value of Routines is held in a string called RoutineText
rout is the NSmangedObject for the entity Routines
routDet is the NSmanagedObject for the entity RoutinesDetails
I have left the commented out code that allows me to add a relationship when both new objects are created.
This is the last thing I have to do in my project but it is driving me insane. I will be eternally grateful for the fix here. Any advice will be appreciated as this is the best knowledge portal there is. Thank You.
NSManagedObjectContext *context = [self managedObjectContext];
// Create a new device
ExcerciseInfo *info = [_fetchedResultsController objectAtIndexPath:indexPath];
//rout = [NSEntityDescription insertNewObjectForEntityForName:#"Routines" inManagedObjectContext:context];
routdet = [NSEntityDescription insertNewObjectForEntityForName:#"RoutinesDetails" inManagedObjectContext:context];
//Add attribute values
//[rout setValue: RoutineText forKey:#"routinename"];
[routdet setValue: info.name forKey:#"image"];
//Create Relationship
[rout addRoutinedetObject:routdet];
Your main problem statement is, I think, here:
I need it so the new object in RoutinesDetails has a relationship with the value that already exists in Routines.
I presume your data model looks like this:
Routine <----> RoutineDetail
i.e. every one routine has one routine detail (one-to-one relationship).
But this does not really make any sense. You could simply include the attributes of RoutineDetail in the Routine entity.
Instead of
desiredValue = routineDetail.routine.value;
you would simply have
desiredValue = routineDetail.value;
Also, please note that your code has a number of problems. The first line is completely unnecessary, just use self.managedObjectContext. Additionally, against the convention you are using Capital initials for variables. Those should be reserved for class names. Your method to add the relationship also does not look right.
You can add a relationship like this, without a method call:
routineObject.detail = detailObject;

How to perform reodering of cells in One-to-many relationship in CoreData

I am learning coreData and I am new it, I have created a one-to-many relationship of boss and employee, (i.e one boss and many employees). So I am showing all the bosses on firstTableView and when the user clicks on the cells, he can view the employees assigned to each boss and also he can add employees to any particular boss. Now I want to reorder the boss cells. So how it should be done?
Edited based on the discussion below
- (void)insertNewObject:(NSString *)fileName
{
Boss *bossName = [NSEntityDescription insertNewObjectForEntityForName:#"Boss" inManagedObjectContext:self.managedObjectContext];
[bossName setName:fileName];
NSManagedObject *lastObject = [self.controller.fetchedObjects lastObject];
float lastObjectDisplayOrder = [[lastObject valueForKey:#"displayOrder"] floatValue];
NSLog(#"%f",lastObjectDisplayOrder);
[bossName setValue:[NSNumber numberWithDouble:lastObjectDisplayOrder + 1.0] forKey:#"displayOrder"];
// Save the context.
NSError *error = nil;
if (![self.managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
}
[Specific Answer To Updated Question]
It would be either ....
[self.boss insertObject:newEmployeeObject inEmployeesAtIndex:0];
Which is a core-data generated method that is part of your Boss subclass if you choose to create subclasses from your model. Or....
NSMutableOrderedSet *employees = [self.boss mutableOrderedSetValueForKey:#"employees"];
[employees insertObject:newEmployee atIndex:0]
It's not that intuitive I know, you can't just make a mutable copy, you have to get a special proxy object from mutableOrderedSetValueForKey.
[Original General Answer]...
Core-data now has the ability to use "Ordered Relationships" which you can specify in your model. If you do so, relationships in your object model will be represented by a new class NSOrderedSet which is a hybrid of an NSArray and an NSSet. By re-ordering the objects in this relationship object and saving the context you will reorder the objects in the database and they will maintain their new order. This kind of ordered relationship tends to be used when there isn't natural ordering attribute on the object. For instance the order simply represents the users preference for ordering a list in the UI.
If on the other hand you have an attribute on one of your objects that describes the order for a collection then you can use that attribute to order the results of an NSFetchRequest by specifying the Sort Descriptors. The value of the attribute would specify the position the object would be in in the results of the NSFetchRequest.
If you are using Ordered Relationships you would need keep the order of the NSOrderedSet for that relationship and the UITableView in sync. If the change was driven from the UI then you respond to the UITableViewDataSource delegate methods such as - (void)moveRowAtIndex:(NSUInteger)sourceIndex toIndex:(NSUInteger)destinationIndex and use the information provided to move the corresponding object to it's new position in the core-data relationship either by using the proxy object from mutableOrderedSetValueForKey: or the Core-data generated accessors of a generated subclass.
If the change to order were driven from the data side you would use the methods on UITableView such as insertRowsAtIndexPaths:withRowAnimation: and moveRowAtIndexPath:toIndexPath: to sync the rows in the UITableView with the changes you were making in the data.
If you are using NSFetchRequests you have a similar task. In this case you respond to user driven changes in the order by updating the sort attributes on your objects to match the new order that is described by the UITableView through the UITableViewDataSource delegate. Or if the ordering changes are starting at the data side you update the UITableView through it's methods to match the changes you are making to the sort attributes on the data. In this case you will be working with the results from the NSFetchResults as an NSArray, you would also have to keep that object in sync until the next time you ran the NSFetchRequest. You could use the same sort descriptor to sort the array, or create an NSMutableArray and use it's methods to move the data to match the table.
Finally if you are using NSFetchRequest you may like to look at NSFetchedResultsController It's job it is to simplify task of syncing a sorted NSFetchRequest and a UITableView. There is good sample code for this in the documentation. In this case you may find that the ordering of the data will take care of itself. For instance say your table is ordered by "assignment date" (i.e. the date at which an employee was assigned to a boss) then simply creating the objects with the correct information would trigger the correct results in the table.
Please note that ordered relationships do not work with iCloud. However in my opinion iCloud doesn't work anyway so that's not a problem.

Resources