I am creating a Reddit client for iOS for learning purposes. I am using CoreData, which I set up by following the CoreData Basics NSScreencast and I used Mogenerator to generate the model files.
I added a breakpoint to catch all exceptions and when I call the insertInManagedObjectContext: class method provided by Mogenerator, the app crashes on the following line:
return [NSEntityDescription insertNewObjectForEntityForName:#"AZRedditAccount" inManagedObjectContext:moc_];
This line is in the model that Mogenerator generated (_AZRedditAccount). When I look at the Variables View, I noticed an odd line which I don't really understand...
What do I need to do to make things work please?
Edit: Here is what I have in my .xcdatamodeld file:
Edit 2: I added the whole project to Github, thought it might help solve the problem: https://github.com/AzizLight/Reddit
If you debug you see that your ManagedObjectContext (MOC) has no assigned PersistentStoreCoordinator (PSC) and therefore not model. In AZRedditDataModel.m you're initializing the PSC, but you're not retaining it (lines 84-109). If there's no model, the context cannot create any entities, because there's no "blueprint".
Related
This is a follow up to an earlier question: Core Data: change delete rule programmatically.
I'd like to rephrase my question, and will do that here.
Briefly, my app allows updating entries from a 3rd party database, but I'd like to keep user annotations. So my workflow is:
iterate over all entities
download external xml and parse it into a new entity
if user annotations, change their relationship from old entity to new entity
delete old entity
During the import, the old entity is in the main context, the new entity is in a temporary import context.
Number 3 gives me problems, if I just change the relationship, then they don't show if I update my UI. If I use the objectID to get the annotation and then change the relationship as follows:
NSManagedObjectID *objectId = oldAnnotation.objectID;
Annotation *newAnnotation = [importContext objectWithID: objectId];
[newEntry addAnnotationObject: newAnnotation];
It's still not working - it's not showing up.
EDIT: if I change the context in the second line to newEntry.managedObjectContext, I get an Illegal attempt to establish a relationship 'foo' between objects in different contexts error.
What am I missing?
UPDATE: After some late-night hair-pulling debugging, I found that I when I was fetching the newEntry, I was actually fetching the oldEntry, therefore none of the changes would show up. The answer below by #Mundi pointed me in the right direction.
Copying the old annotations worked using my code above, followed by copying the attributes. For some user input with relationships in itself, I had to do a "Deep Copy", which I found here: How can I duplicate, or copy a Core Data Managed Object?.
I think creating a new entity and deleting the old one is a problematic strategy. You should try to properly update the existing entities and only create new ones if they do not yet exist.
Whenever I need an object from a different context, I fetch it. That being said, your object id code should work. However, there could be all sorts of other glitches, that you should check:
Did you save the importContext?
Did you save its parent context, presumably the main context?
Was the modified object graph saved to the persistent store?
Are you checking the results after you have saved?
I have first project that dealing with coreData. I followed this tutorial. Everything was fine until I created new project with new core data models that has same fields and same structure to have deeper understand. However, I couldn't figure out where the variable self.entityName in class SDDateTableViewController.m got assigned value. Because the variable self.entityName in my new project is getting nil in method -(void)loadRecordsFromCoreData in SDDateTableViewController.m class.
Note: I did try copy every single line of code to my project, but the variable self.entityName is still nil
Please help me explain this issue.
It is define in the Storyboard on the SDDateTableViewController scene on "User defined runtime attributes".
I'm following this book tutorial to learn core data (Apress Pro Core Date for iOS 2nd Edition). And I thought I was following correctly but I ran into an error I dont know how to solve. The code is posted here.
I'm interested not only in the correction to the problem, but the steps you took to find out what the problem is.
The error message is "Cannot create an NSPersistentStoreCoordinator with a nil model", so that indicates there's something wrong with your managedObjectModel method.
First off, you named your model file as "Model", but the method is looking for one called "OrgChart", so either rename your model file or change the name in the managedObjectModel method.
Afterwards, it still doesn't work and setting a breakpoint in the managedObjectModel method reveals that it is actually never called. That's because you have a typo there, you've called the method mangedObjectModel instead of managedObjectModel. After adding the missing a, your app should run.
I have following Problem:
First up... i use MagicalRecord for the whole CoreData thing
I have two Entities: A and B
They have a relation between each other
I create one instance of Entity A
I create several instances ob Entity B and set the relation
I don't call [[NSManagedObjectContext defaultContext] save];
It's fine
The relation is ok... I can check it using the findByAttribute method
If I call this save then the relation is destroyed...
the same check using findByAttribute does not find results any more
I have absolutely no clue what I am doing wrong or if it's a bug in CoreData / MagicalRecord...
I made a sample project showing the problem.
https://github.com/bliblablo/MagicalRecordsProblem
You can see the problem by following this steps:
click "create"
click "add"
click "check"
see the log output for results
click "save"
click "check" again and see the problem in the log :)
Any help is really appreciated!!!
Thanks a lot!
Sounds like the problem I was having with temporary ObjectID's not getting refreshed in the default context.
See my answer on NSPredicate not executed for details.
If you haven't already sorted it out, try checking the ObjectID of the NSManagedObject instances at various points. Especially if you (or Magical Record) are using the object as part of an NSPredicate to do the later fetches.
I think this is not MR bug. It is a bug from core data. See this post.
http://wbyoung.tumblr.com/post/27851725562/core-data-growing-pains
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.