Illegal attempt to establish a relationship 'xyz' between objects in different contexts - iphone

I am using Apple's CoreDataBooks sample application as a basis for pulling data into a secondary managed object context in the background, and then merging that data into the primary managed object context.
The data I am pulling in is a Book entity with a to-one relationship with an Owner entity (called "owner"). The Owner entity has a to-many relationship with the Book (called "books").
My data is an XML document of the form:
<Owner>
<Name>alexpreynolds</Name>
<ID>123456</ID>
</Owner>
<Books>
<Book>Book One</Book>
<Book>Book Two</Book>
...
<Book>Book N</Book>
</Books>
Book One through Book N are associated with one Owner ("alexpreynolds, 123456").
I am parsing this into an Owner instance and an NSMutableSet made up of Book instances.
When I attempt to save the first time, it saves fine and the merged data shows up in the table view.
On the second save, however, when the XML content contains a new book, it doesn't work.
Here's what happens:
I then attempt to load in an XML document that contains a new Book not already in the primary managed object context. The new Book is using the same Owner as that which is associated with the other Books.
I have routines that pick out this unique Owner managed object (which I already have in my primary managed object context) and the unique Book that is not found in the primary MOC.
From this, I create a new Book object in the secondary MOC, and I set its "owner" relationship to point to the unique Owner I found in the primary MOC.
When I save, I get the following error:
*** Terminating app due to uncaught
exception 'NSInvalidArgumentException',
reason: 'Illegal attempt to establish a
relationship 'owner' between objects in
different contexts
(source = <Book: 0x7803590>
(entity: Book; id: 0x7802ae0 <x-coredata:///
Book/t527F06B2-3EB5-47CF-9A29-985B0D3758862>
; data: {
creationDate = 2009-10-12 06:01:53 -0700;
name = nil;
nameInitial = nil;
operations = (
);
owner = nil;
type = 0;
}) ,
destination = <Owner: 0x78020a0> (entity:
Owner; id: 0x3a56f80 <x-coredata://043AF2F0-1AD0-
4078-A5E8-E9D7071D67D1/Owner/p1> ; data: {
books = "<relationship fault: 0x7801bf0 'books'>";
displayName = alexpreynolds;
ownerID = 123456;
}))'
How do I create a new Book entity in the secondary MOC, so that I can still associate it with a pre-existing Owner in the primary MOC?

You can't have relationships between objects in different managed object contexts. So one way of getting around that is to bring the object into the managed object context.
For example:
NSManagedObject *book = // get a book in one MOC
NSManagedObject *owner = // get an owner in a different MOC
[[owner mutableSetValueForKey:#"books"] addObject:[owner.managedObjectContext objectWithID:[book objectID]]];
So what you're doing is actually fetching the Book into the same managed object context with owner. Keep in mind, though, that this is only possible if book has already been saved. The managed object context is going to look for the object in the persistent store, so it has to be saved first.

I had the same problem and this sentence helped me to solve the error.
You can't have relationships between objects in different managed
object contexts. So one way of getting around that is to bring the
object into the managed object context.
Here's my code (I replaced the variables to make it work for you):
// Have the owner object and get the managedObjectContext
Owner *owner = [[DataFunctions alloc] getCurrentOwner];
NSManagedObjectContext *context = [owner managedObjectContext];
// Create a book and use the manageObjectContext of owner
Book *book = [NSEntityDescription insertNewObjectForEntityForName:#"Book" inManagedObjectContext:context];
[newQuote setValue: "Book Title" forKey:#"title"];
[newQuote setOwner:owner];
I hope this helps :)

Here, you are trying to establish a relation between two objects which are fetched/created with a different context. Core data won't allow you to establish such relations. To achieve this, you should fetch the second object (you are trying to make a relationship with) with the context of the first object with objectID. Now you should be able to establish a relation between these two objects. For example:
MagicalRecord.saveWithBlock({[unowned self] (localContext: NSManagedObjectContext!) in
let user = User.MR_createEntityInContext(localContext)!
.....
.....
}) //here local context stores data on end of block itself
MagicalRecord.saveWithBlock({[unowned self] (localContext: NSManagedObjectContext!) in
let address = Address.MR_createEntityInContext(localContext)!
.....
.....
let user = localContext.objectWithID(self.user!.objectID) as! User
user.address = address
})
Hope this will help you!

As the error says, you're not allowed to have a relationship in one Core Data object whose value is set to another object held in a different context. One way you can get around this is to wait until after you save the new object and merge it back into the primary context, then set the owner relationship as appropriate (since both objects are now in the same context, there's no problem with that).

book *book = [mainContext ........] //Get book from default context
NSManagedObjectID *objectId = [book objectID];
Book *tmpBook = [tmpContext objectWithID:objectId]; //Now book has the legal relationship

Swift version...
context.insert(objectFromOtherContext)

Related

Why would my data relationship is not created in CoreData?

I have two entities in my CoreDataModel that should be linked together with a too-many relationship on one wary and to-one on the other way.
These are the CityEntity and the WeatherEntity.
The CityEntity can have multiple WeatherEntity, but not the opposite.
I have two main errors that I would like to solve.
When saving the data, the NSManagedObjectContext save as many CityEntity as I have WeatherEntity, which is wrong as I am expecting only one CityEntity.
When saving WeatherEntity, the relationship with the CityEntity is not created.
This is the code I am using to save the data.
func saveForecast() {
let cityModel = CityModel(
name: "Paris",
weather: [WeatherModel(temp: 12.65), WeatherModel(temp: 12.43)]
)
let cityEntity = CityEntity(context: persistentContainer.viewContext)
cityEntity.name = await cityModel.name
for weather in await cityModel.weathers {
let weatherEntity = WeatherEntity(context: persistentContainer.viewContext)
weatherEntity.temp = weather.temp
weatherEntity.city = cityEntity
}
do { try persistentContainer.viewContext.save() }
catch { print(error.localizedDescription) }
}
These are the result that I get saved in my CoreDataModel, which is wrong as I have 3 CityEntity instead of one, and no relationships between my WeatherEntity and the expected CityEntity.
You can see that there is not CityEntity saved in place of the name when looking at the `WeatherEntity.
This is the way I set the relationship in the CoreDataModel:
It seems that you (accidentally?) set the parent entity of WeatherEntity to CityEntity. That makes WeatherEntity a “sub-entity” of CityEntity, and the corresponding WeatherEntity class a subclass of CityEntity.
Then every instance of WeatherEntity is also an instance of CityEntity, and that explains why there are three instances of CityEntity in total after running your code.
Resetting the parent entity to “No Parent Entity” in the Core Data model inspector, re-generating the managed object subclasses, and cleaning all application data should solve the problem.

Deleting multiple objects from a local Realm Database on iOS

I am finishing up an app utilizing a local Realm database with three models. When I delete an object from the main master model, the connected objects in the child models are supposed to all delete, but only one of the objects in each model deletes. See the attache diagram.
Not sure where to go next. Any help will be greatly appreciated!
Thanks!
Blessings,
—Mark
This is the normal behavior of realm js.
When you delete one element of the master model, the child element related is still present on the database.
The only solution is to get all child element and delete them first.
For example if you have a Master model named "Order" and a child model named "customer" :
/** if you use realm-js **/
const order = realm.objects('Order').filtered(`id = 1`)[0]
realm.write(() => {
realm.delete(order.customer)
realm.delete(order)
})
also you can find related related post with the same question
here or
here
Anyone else with this question, knowing the child databases must be deleted first, the solution became self-evident. Thanks again for pointing this out!.
//get the object at indexPath.row
let a = self.master![indexPath.row]
let id = a.Id //master's record ID
let n = realm.objects(Child1.self).filter("Id = %#", id)
let r = realm.objects(Child2.self).filter("Id = %#", id)
//Delete object at indexPath.row
try! realm.write {
realm.delete(n)
realm.delete(r)
//delete last
realm.delete(a)
}
Blessings,
—Mark

"The property $propertyName is part of the object's key information and cannot be modified." on navigation property with shared composite key

To preface this question: my problem is not because I'm directly setting a key property in my model entity object (which is the cause of the issue in other search results for the same exception message).
I'm making heavy use of composite keys in my application, here's a simplified version of my current DB schema (key fields in *asterisks*):
Tenants( *TenantId*, ... )
Categories( *TenantId*, *CategoryId*, ... )
Documents( *TenantId*, *DocumentId*, CategoryId, ... )
The Documents table has FK relationships with both Tenants and Categories, both using the same Documents.TenantId column. The Documents.CategoryId column is NULLable.
When I do something like this, I get the exception:
Tenant tenant = GetTenant( 123 );
Document doc = tenant.Documents.First();
Category newCategory = new Category();
newCategory.TenantId = 123;
dbContext.Categories.Add( newCategory );
doc.Category = newCategory; <-- exception is thrown on this line, without calling dbContext.SaveChanges() at all.
I believe the exception is because setting Category on the Document instance causes the TenantId property to be set indirectly by EF (because it's part of the Documents -> Categories FK association.
What is the solution?
Workaround Update
I'm able to hack it by creating the new Category entities then saving them, to get the IDENTITY values back, then setting the Document properties directly:
Tenant tenant = GetTenant( 123 );
Document doc = tenant.Documents.First();
Category newCategory = new Category();
newCategory.TenantId = 123;
dbContext.Categories.Add( newCategory );
dbContext.SaveChanges();
doc.CategoryId = newCategory.CategoryId
dbContext.SaveChanges();
But ideally I'd like this to work in a single call to SaveChanges() and using the Entity Model Navigation Properties instead of scalar attribute properties.
For this initial problem, I worked-around it using the "Workaround Update" I posted to my original posting.
However this problem happened again for a different entity type (again, with a composite key involved in a foreign-key) and I noticed that EF throws the exception even if you call dbContext.Entry() on any entity in the graph while the new entity is in the Added state - but it does not throw the exception again if you re-call Entry() or even SaveChanges(), and in fact it saves the new entities correctly in spite of the initial exception - so I'm thinking this might just be a bug in EF.
Here's essentially what I have now:
Tenant tenant = GetTenant( 123 );
Document doc = tenant.Documents.First();
Category newCategory = new Category();
newCategory.TenantId = 123;
dbContext.Categories.Add( newCategory );
doc.CategoryId = newCategory.CategoryId
try {
dbContext.Entry( doc );
}
catch(InvalidOperationException) {
}
dbContext.SaveChanges();
It's ugly, but works - and avoids having to call SaveChanges twice.

Recursively applying MergeOption.NoTracking

I want to get an object from entity framework and return it along with all its related objects to the user. When I set the MergeOption to MergeOption.NoTracking, I do get the first related objects even after the entities/context object get destroyed. But when I try to get the related objects of the related objects, I get an exception saying that the entities object doesn't exists any more! I tried setting the MergeOption on all entities that are retrieved, but that didn't work. Any idea how to solve this problem?! Here is my code:
MyFirstObject myObject;
using (var entities = new MyEntities())
{
entities.MyFirstObject.MergeOption = MergeOption.NoTracking;
entities.MySecondObject.MergeOption = MergeOption.NoTracking;
entities.MyThirdObject.MergeOption = MergeOption.NoTracking;
myObject = entities.MyFirstObject.First();
}
myObject1.MySecondObjects..... // Works fine.
myObject1.MySecondObjects.MyThirdObjects.... // Throw an exception.
MergeOption has no effect on this. MergeOption just tells EF how to handle materialized entities - NoTracking means that context must not track entities for changes. You must use eager loading.
myObject = entities.MyFirstObject
.Include("MySecondObjects.MyThirdObjects")
.First();

How to relate entities that are not directly mapped through a navigation property

I have an object that has been populated with the contents of four different related entities. However i have another entity in which i cannot include as part of the query due to it not being related in the navigation properites directly to the IQueryable table i am pulling. The entity i am trying to include is related to one of the four different entities that have been included successfully.
Is there a way to include(during db hit or afterwards) this entity as part of the overall object i am creating?
Here is an example of what my calls look like to build the CARTITEM object:
public List<CARTITEM> ListCartItem(Guid cartId)
{
//Create the Entity object
List<CARTITEM> itemInfo = null;
using (Entities webStoreContext = new Entities())
{
//Invoke the query
itemInfo = WebStoreDelegates.selectCartItems.Invoke(webStoreContext).ByCartID(cartId).ToList();
}
//Return the result set
return itemInfo;
}
here is the selectCartItems filter(Where i would normally do the includes):
public static Func<Entities, IQueryable<CARTITEM>> selectCartItems =
CompiledQuery.Compile<Entities, IQueryable<CARTITEM>>(
(cart) => from c in cart.CARTITEM.Include("ITEM").Include("SHIPPINGOPTION").Include("RELATEDITEM").Include("PROMOTION")
select c);
from this i have my CARTITEM object. Problem is i want to include the PROMOTIONTYPE table in this object, but since the CARTIEM entity doesn't have a navigation property directly to the PROMOTIONTYPE table i get an error.
Let me know if you need any more clarification.
Thanks,
Billy
You can use join and if it is the same database and server it should generate the join in SQL and do it all in one call...
LinqToEnties join example