strange relationship to nsmanagedObject - core-data

i have multiple NSmanagedObject from the same entity ( we call PersonEntity).
This entity have a relationship "to-many" for another entity (we call BusinessEntity).
when i populate my store, i create the BusinessEntity managedObject.
After i add this BusinessEntity managedObject to my first PersonEntity managedObject.
[(Person *)entity1 addBusinessObject:businessEntity];
[(Person *)entity2 addBusinessObject:businessEntity];
the addBusinessObject function ( generate by XCode ) :
- (void)addBusinessObject:(NSManagedObject *)value {
NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
[self willChangeValueForKey:#"business" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
[[self primitiveValueForKey:#"business"] addObject:value];
[self didChangeValueForKey:#"business" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
[changedObjects release];
}
this work but only just after the populate.
If i save the store, only the relationShip between the entity1 and the businessEntity exist.
I have no relationShip between entity2 and businessEntity.
Really strange
PS: my two Entity are subclass of NSManagedObject for use with undefined property, and transient property.
Thanks for your help

On the apple documentation Core Data Programming Guide
i have read this important text :
Important: You must define
many-to-many relationships in both
directions—that is, you must specify
two relationships, each being the
inverse of the other. You can’t just
define a to-many relationship in one
direction and try to use it as a
many-to-many. If you do, you will end
up with referential integrity
problems.
Now it's work

Related

A core data model object deleted in one MOC can still be used in another

This is a test on part of Apple's Core Data PG, which I quote here
You started with a strong reference to a managed object from another object in your application.
You deleted the managed object through the managed object context.
You saved changes on the object context.
At this point, the deleted object has been turned into a fault. It isn’t destroyed because doing so would violate the rules of memory management.
Core Data will try to realize the faulted managed object but will fail to do so because the object has been deleted from the store. That is, there is no longer an object with the same global ID in the store.
So I setup a test project to see if it is the real case.
I'm using MagicalRecord to save some troubles creating MOCs, the code is based on a Core data model Class named "People"
#interface People : NSManagedObject
#property (nonatomic) int64_t userID;
#property (nullable, nonatomic, retain) NSString *name;
#end
In the test part, I wrap the MOCs MagicalRecord created into backgroundMOC and UIMOC so that those who are not familiar with MagicalRecord won't be confused.
UIMOC is BackgroundMOC's child and will merge backgroundMOC's changes by listening to NSManagedObjectContextDidSaveNotification backgroundMOC send out.
The "saveWithBlockAndWait" is just a wrapper around "performBlockAndWait". So here comes,
[[self backgroundMOC] MR_saveWithBlockAndWait:^(NSManagedObjectContext * _Nonnull localContext) {
People *people = [People MR_createEntityInContext:localContext];
people.userID = 1;
people.name = #"Joey";
}];
People *peopleInMainThread = [People MR_findFirstInContext:[self UIMOC]];
NSLog(#"Before delete, name = %#", peopleInMainThread.name);
[[self backgroundMOC] MR_saveWithBlockAndWait:^(NSManagedObjectContext * _Nonnull localContext) {
People *people = [People MR_findFirstInContext:localContext];
NSLog(#"Deleting, name = %#", people.name);
[localContext deleteObject:people];
}];
NSLog(#"After delete, name = %#", peopleInMainThread.name);
[[self UIMOC] save:nil];
NSLog(#"After save UIMOC, name = %#", peopleInMainThread.name);
The NSLog result is
Before delete, name = Joey //As expected
Deleting, name = Joey //As expected
After delete, name = Joey //Shouldn't it be nil already?
After save UIMOC, name = null //As expected
This result seems to state that Merge from parent MOC won't make model objects fault, which could lead to some hard-to-find bugs or instead tedious checking codes everywhere.
Again with the people object. I'll have to do things like this
- (void)codesInSeriousApp
{
[[self backgroundMOC] MR_saveWithBlockAndWait:^(NSManagedObjectContext * _Nonnull localContext) {
People *people = [People MR_createEntityInContext:localContext];
people.userID = 1;
people.name = #"Joey";
}];
__block People *people = nil;
[[self UIMOC] performBlockAndWait:^{
people = [People MR_findFirstInContext:[self UIMOC]];
}];
[self sendHttpRequestViaAFNetworking:^{
//this block is executed on main thread, which is AFNetworking's default behavior
if ([[self UIMOC] existingObjectWithID:people.objectID error:NULL])//[people isFault] would be NO here, and people's properties stay still.
{
//do something
}
else
{
//the people object is gone
//maybe some codes on another thread deleted it and save to the backgroundMOC
//the UIMOC merge the changes sent by notification, but the people object is still NOT FAULT!
}
}];
}
As far as I can tell, for any model non-fault object in a specific MOC, say MOCA, the object won't be fault until [MOC save:&error] called all the way down to the persistent store.
What really confuse me is, if Another MOC, already know that the object is fault by doing the saving chain, and MOCA merged changes that very MOC send out, how come the object in it is still non-fault?
Am I misunderstood or anything? Any reply would be appreciated.
Thx in advance :)

Why does an unsaved managedObject lose its managedObjectContext

Currently, I am developing an app to book cars. All booking related data are stored in an entity 'Bookings'. As some attributes of 'Bookings' or relationships between 'Bookings' and other enties are mandatory I decided to add all managedObjects of entity 'Bookings' to their own managedObjectContext. This context will also be stored in a separate variable to avoid losing it. This works fine unless I'll sign (enterprise store or adhoc) my app and deploy it. ARC is enabled.
Class Bookings interface
#interface Bookings : NSManagedObject {
#private
NSManagedObjectContext *mContext;
}
#end
Class Bookings implementation
#implementation Bookings {
+ (Bookings *)booking {
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:concurrencyType];
[context setPersistentStoreCoordinator:[self persistentStoreCoordinator]];
Bookings *object = (Bookings*)[[NSManagedObject alloc] initWithEntity:[self entityForName:pName] insertIntoMarenagedObjectContext:context];
[object setSomeReservationData:...];
...
// here I store the context in an ivar of my booking object
[object->mContext = context];
return object;
}
}
At this state the Booking object will not be saved!
Class BookingsVC
Bookings *booking = [Bookings booking];
NSLog(#"Context: %#", [booking managedObjectContext]);
Nothing saved or altered but context is null.
Console output on device (adhoc signed and deployed via iPhone-Configurator or Testflight)
... Context: (null)
Console output on simulator or device (adhoc signed but installed via Xcode)
... Context: <NSManagedObjectContext: 0x893c520>
So why does an unsaved managedObject lose its managedObjectContext and how can this be avoided? Is it a bug or the expected behavior?
Your context is nullified at the end of your function. see here
Your object is disowned by the context making all its properties null, in debug mode there exists an autorelease pool keeping the context from being deallocated.

Using selected NSManagedObject across different controllers

I have an entity called Practice and I use a View Controller called SelectorViewController to select one of the practices, selectedPractice. I then return selectedPractice to a view Controller called RegularViewController where I display some of the selectedPractice attributes. All of this works fine. However the app has a number of other View Controllers which can be reached by modal segues from instances of RegularViewController. As a result, if I leave and then come back to RegularViewController, selectedPractice is reset as null. I would also like to save selectedPractice so that it is available at app initialisation if it has previously been set in SelectorViewController. How do I achieve this by making selectedPractice persistent across the app, and available at runtime?
Regards
Thanks to the post above, which was great, I managed to sort it. Here is my code, which may be very clumsy, but it works.
Firstly, as I loaded the fetchedObjects into a PickerView in SelectorView Controller, I set an attribute "isSelectedPractice" to "NO" with the following code:
for (Practice *fetchedPractice in [self.fetchedResultsController fetchedObjects]) {
[fetchedPractice setValue:#"NO" forKey:#"isSelectedPractice"];
[self.managedObjectContext save:nil];
I then identified for the selected Practice:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
Practice *practice = [[self.fetchedResultsController fetchedObjects] objectAtIndex:row];
self.selectedPractice = practice;
NSLog(#"The '%#' practice was selected using the picker", self.selectedPractice.name);
}
as the view Segue'd back to RegularViewController I set the isSelectedPractice attribute for selectedPractice to YES. I kept it this late as I didn't want more than one selection in the PickerView to result in multiple objects with isSelectedPractice YES.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"SavedPractice Segue"])
{
[self.selectedPractice setValue:#"YES" forKey:#"isSelectedPractice"];
[self.managedObjectContext save:nil];
NSLog(#"Setting SelectedPractice as '%#' in RegularViewController with isSelectedPractice as '%#'",self.selectedPractice.name,self.selectedPractice.isSelectedPractice );
RegularViewController *rvc= segue.destinationViewController;
rvc.delegate = self;
rvc.selectedPractice = self.selectedPractice;
}
else {
NSLog(#"Unidentified Segue Attempted!");
}
}
I then set the following Predicate in the setupFetchedResultsController method of RegularViewController:
request.predicate = [NSPredicate predicateWithFormat:#"isSelectedPractice = %#", #"YES"];
Many thanks for the help
Without seeing your actual project, one way I know will work but might be a little too round a bout would be to add an attribute "isSelectedPractice" to your entity. You could make it a BOOL, but I've had mixed results with BOOL's in Core Data, I prefer to just leave it as a NSString and set it to "yes" or "no". Then when you pull it down, modify it or add it to core Data as a entity with isSelectedPractice set to "yes". Then in your other controllers, do a
if (self.managedObjectContext == nil) {
self.managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
then do a fetch request to get entities with a predicate which is looking for isSelectedPractice equaling "yes". If you need actual code samples on how to do this let me know and I'll edit them in.

How do you get a new Core Data Entity attribute to be reflected in the NSManagedObject for that Entity?

I'm trying to implement a getter on one of my db classes. But when I execute the following line of code, where "obj" is an NSManagedObject:
return [obj valueForKey:#"activationData"];
I get the following NSUnknownKeyException:
'[ valueForUndefinedKey:]: the entity Blueprint is not key value coding-compliant for the key "activationData".'
I just recently added a String attribute named "activationData" to my "Blueprint" entity using Xcode. But when I run the app the NSManagedObject that represents Blueprint entities does not include the new "activationData" attribute, which apparently is the cause of the crash.
The NSManagedObject looks like this, but I expected it to show the new Attribute along with the createDate, name and order attributes:
<NSManagedObject: 0x5138c90> (entity: Blueprint; id: 0x513a2e0 <x-coredata://8C586BB8-B9E7-4FD7-84CB-5CE66FB221E6/Blueprint/p2> ; data: {
createDate = "2012-02-21 15:49:00 +0000";
name = "Feb 17 test";
order = 2;
})
Fyi, user1226119's answer (below) reminded me that I used the Organizer to extract the sqlite db from my device and inspect it with SQLite Manager to verify things. Sure enough there is still no new activationData field in the Blueprint table. The table looks the same as it always did.
I think I must have missed some necessary step for adding a new Attribute to an existing db Entity.
Your model has not change in your app. You must delete your old application and re-deploy your app on your device.
The solution was to update the pathForResource method call in the code that returns the NSManagedObjectModel. I had indeed created a new xcdatamodel version of the db before adding the attribute, but apparently you are supposed to refer to it using the following code, which retrieves the model your app will use.
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
NSString *path = [[NSBundle mainBundle] pathForResource:#"MyDB Version5" ofType:#"mom" inDirectory:#"ASSIST.momd"];
NSURL *momURL = [NSURL fileURLWithPath:path];
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];
return managedObjectModel;
}
I had to put the new database version's name ("MyDB Version5") as the pathForResource parameter. Previously it was "MyDB Version5".

Using GameKit to transfer CoreData data between iPhones, via NSDictionary

I have an application where I would like to exchange information, managed via Core Data, between two iPhones.
First turning the Core Data object to an NSDictionary (something very simple that gets turned into NSData to be transferred).
My CoreData has 3 string attributes, 2 image attributes that are transformables.
I have looked through the NSDictionary API but have not had any luck with it, creating or adding the CoreData information to it.
Any help or sample code regarding this would be greatly appreciated.
I would recommend that you convert the Core Data objects to an intermediate format like JSON before pushing it over the wire. I have written up the code on how to transform NSManagedObject instances into and out of JSON in this other post:
JSON and Core Data on the iPhone
NSManagedObject doesn't conform to the NSCoding Protocol so you can't convert a managed object straight to data.
Instead, you just need to add a method to the managed object subclass that returns a dictionary with the instance attributes and then on the receiver side, use those to create a new managed object in the local context.
Edit:
From comments:
Currently I have for the sending
side..
NSData* data;
NSString *str0 = [NSString stringWithFormat:#"%#",[[person valueForKey:#"PersonName"] description]];
NSString *str1 = [NSString stringWithFormat:#"%#",[[person valueForKey:#"alias"] description]];
NSMutableDictionary *taskPrototype = [NSMutableDictionary dictionary];
[taskPrototype setObject:str0 forKey:#"PersonName"];
[taskPrototype setObject:str1 forKey:#"alias"];
data = ?????;
//I do not know what to put here... [self mySendDataToPeers:data];
on the receiving side I have...
NSMutableDictionary *trial = [[NSMutableDictionary alloc] initWithData:data];
NSString *str0a = ???? NSString *str1a = ????
//I dont know what to put after this to retrieve the values and keys from the dictionary
You would simply reverse the process to create a managed object on the receiver.
NSMutableDictionary *trial = [[NSMutableDictionary alloc] initWithData:data];
NSManagedObject *person=[NSEntityDescription insertNewObjectForEntityForName:#"PersonEntity" inManagedObjectContext:moc];
[person setValue:[trial objectForKey:#"PersonName"] forKey:#"PersonName"];
[person setValue:[trial objectForKey:#"alias"] forKey:#"alias"];
.. and you're done.

Resources