CoreData compare Error during Save - ios

I'm having some issues with saving a new object/editing an existing one, from a One-To-Many relationship in CoreData.
I have a class Patient that has a To-Many relationship with class Indication.
What's been happening is:
First time I create an Indication Object from a normal workflow, it all seems to save alright, but if I try to access it from an Edit screen, it crashes with a SIGABRT and the following error
-[Patient compare:]: unrecognized selector sent to instance 0x7fa991893ca0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Patient compare:]: unrecognized selector sent to instance 0x7fa991893ca0'
The same happens if I try to create a new one from the NSFetchedResultsController that shows the list of Indication Objects.
I've tried to create the relationships between them in two ways:
indication = [NSEntityDescription insertNewObjectForEntityForName:#"Indication"
inManagedObjectContext:appDelegate.managedObjectContext];
indication.patient = patient;
and through the GeneratedAccessors from CoreData:
[patient addIndicationObject:indication];
and I validate both objects during viewDidLoad to avoid creating a new Indication if I'm editing an existing one. But in both cases I get the same error.
Haven't been able to find any posts related to this matter so far. Please help.

After a few days researching this with no success, finally I've managed to find a post that managed to shine some light on the problem.
It happens that the NSFetchedResultsControllerDelegate seems to "lock" the NSManagedObjectContext and that's why I couldn't add or change anything within the same context.
I just had to set the delegate to nil once I left the screen and everything seems to work just fine now.
Thanks for the solution on this post:
Core data: Serious application error

Related

Core Data: Illegal attempt to establish a relationship 'statusmedia' between objects in different contexts

I am using Core Data on my app. After a light weight migration and code upgradation to swift 4.0, I am facing few issues.
Issue 1
Mutating a managed object 0x7fd0881de320 (0x7fd0884589b0) after it has been removed from its context.
Issue 2
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Illegal attempt to establish a relationship 'statusmedia' between objects in different contexts (source = (entity: Status; id: 0x10f6dc280
Termination issue is very serious.
Note that I have used only one Context (default one and not private)
Let me know if you want more information from me...
Any help would be greatly appreciated.
Many may happen unfortunately to lead to this but top candidates to check would be:
In your model there are delete rules on relations. A "cascade" option should delete the object(s) in relation when deleting this object. If this is not expected such an error could easily be produced.
You are deleting an object but then still use it. It is a logical bug but can be easily confirmed by marking deleted managed objects (put the IDs of deleted objects into some array) and then checking if the failed object is marked (it's ID exists in that array).
You are using multiple contexts but not aware of it. Some tools like fetch result controllers are potential candidates.
There are still other possibilities but I would start with these mentioned to begin with.

swift core data get objects from one to many relation

I have two entities and a simple one to many relation, Activity and CountedObject entities, and the relation Counted object can have multiple Activities
So in my code I was able to get the list of counted objects and now I want to calculate the sum of amount from activities related to specific counted object. Here is my code
let countedObject = countedObjects[indexPath.row]
let activities = countedObject.activities?.allObjects as! [Activity]
the problem is when I try to get activities I have an error
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CountedObject activities]: unrecognized selector sent to instance 0x60000009d470'
Seems like the relation is not correct or something
To set relation correctly in CoreData just making relationship in table is not enough you have to set all relation object using an object ex.
countedObject.activity = activityObject
Then after you can access activityObjects from countedObject.activites

Core Data Many to Many relation insert data error

I am having an issue with core data many to many relationship. I have two entity Menus <<---->> Categories. I can insert data on both the entities. But when I tried to establish a relationship between them, I used this code:
[menu addToCategoriesObject:catagory];
And it crashes with the following error :
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI addToCategoriesObject:]: unrecognized selector sent to instance 0x8fca680'
Your help is appreciated. Also, if there is any good tutorial on Core Data many to many relationship, please share it.
Thanks.
menu seems to be an NSDictionary object. NSDictionary does not know the method addToCategoriesObject.
You need to establish the relationship by calling the methods on your generated NSManagedObject subclasses, for example something like addManyCategories:(NSSet *)value.
The problem was I kept my data in NSDictionary instead of NSManagedObject subclass object. NSDictionary changed its format. So when I tried to add it by addToCategoriesObject method it crashed!!

getObjectsResult.objects led to error

With QuickBlox, one can create custom objects. All went fine but now I want to display the returned records from one of my custom class. I can see that the records are returned but I cannot grab them to display on a UITableView. I tried saving the result of getObjectsResult.objects into a NSMutableArray and then used an "objectForKey" construction but although the records were saved in my array, I got the error :
2013-04-25 22:09:09.700 People[897:12b03] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[QBCOCustomObject objectForKey:]: unrecognized selector sent to instance 0x86c2520'
I will appreciate any help to help me fix this. For more check out http://quickblox.com/developers/SimpleSample-customObjects-ios.
Thanks!
-kisimi
For predefine custom object table field, you don't need to use "objectForKey" to get value from field. you need to only use QBCOCustomObject's property for example below...
For getting message ID just use [[QBCOCustomObject]ID].
and for custom filed value [[QBCOCustomObject]objectForKey:#"customFieldName"].

Strange CoreData error ... value returned from insertNewObjectForEntityForName: appears to be corrupt

I have a very strange error happening in an App that has been working for a long time.
I can no longer create one of my entities in my CoreData model.
When I create one particular entity in my model and try to print it using NSLog( #"%#", obj ), I get this strange message:
2011-11-08 13:03:05.936 iLearnFast[31541:15503] -[__NSCFNumber objectID]: unrecognized selector sent to instance 0xa069e20
When I loop over the attributes / relationships for this object and print them out, one particular one to one relationship returns a strange value from [obj valueForKey:]. The value it returns is the same pointer / object that is mentioned in the above error message.
I thought I might have been corrupting memory somewhere, but I inserted the code to create the entity at the very beginning of my executable as soon as datastructures are initialized, and I get the same problem. I am extremely confident that I have not made any memory errors at this point (and a memory error would be more random ... I can create thousands of objects, and always the same entity has the same problem with the same relationship, and no other entities ever have a problem).
After narrowing down the problem to this one relationship, I found that I could make the error go away by renaming the relationship to anything else. The relationship has been called "file" since my App was created.
I can make my code work again by renaming the property, although it messes up my automatic lightweight migration, but now I have to deal with figuring out how to do a more complicated migration.
If anyone has any ideas as to what might be going wrong, I would really appreciate it.
This is baffling me, and really feels like a bug in Apple's SDK.
I'm currently using XCode 4.2 and tried both the SDKs for both iOS5.0 and iOS4.3 and both had the same behaviour.
Ron
2011-11-08 13:03:05.936 iLearnFast[31541:15503] -[__NSCFNumber
objectID]: unrecognized selector sent to instance 0xa069e20
I guess you have a leak in your code. This line means that you trying to access objectID property which contains in your custom NSManagedObject, but for some reason that object no longer lives. Try to check your code on memory leaks.
I saw this same issue arise in a Swift project using Core Data. The issue only raised it's head when I pushed the app onto a device (it had been working fine in the simulator for some time).
The issue centred around a relationship between two entities. To illustrate see problem image:
After looking around SO for sometime I decided to rename the relationships, captain and player. See fix image:
Only after renaming both 'ends' of the relationship did the error go away.
I had same error message and in my case reason was that I had relation property teacher and created read-only property isTeacher.
This issue caused by Objective-C name conventions: CoreData was confused in getting teacher property. Instead of accessing real relation and giving me real object it taking isTeacher getter with BOOL type, cast it to NSNumber and try to deal with it like CoreData relation and call objectID.
After renaming isTeacher to isTeacherLicence problem gone.

Resources