You have entered the name of the controllerWillChangeContent method incorrectly: the final word should be content not context: - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { [self.tableView beginUpdates]; } ...

When you call rollback it only reverts unsaved changes in that context. In the first block of code you have already saved those changes and thus rollback will not do anything. When you called save in the first code block all of the changes were committed to the parent context...

As has been pointed out, for table and collection views, the best bet is NSFetchedResultsControllerDelegate. Another mechanism is to register for this (or your custom) notification NSNotificationCenter, e.g. for the original notification: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateUI:) name:NSManagedObjectContextDidChangeNotification object:nil]; Best do this in viewDidAppear. Don't forget to remove the observer in...

This is a common error. It means that that you cannot loop a collection and modify it at the same time. Say you wish to loop and find objects to delete. Don't delete in the loop, plAce things to be deleted in a temporary collection, and delete them after the...

"for every object in a persistent store there may be at most one corresponding managed object associated with a given context" You misunderstand. It means that for any object in the data store, each managed object context will have only one managed object instance in memory for that object....

If you are wanting to react to deletions then you should be listening for NSManagedObjectContextWillChangeNotification and watch for the NSDSeletedObjectsKey come through as part of the notification. That is the last chance before deletion to deal with them.

One possibility is that your persistent store has become corrupted and is in an inconsistent state. If this happens an error code is generated which Magical Record does not necessarily deal with. This can be the source of a number of difficult-to-repeat apparently-random crashes related to Magical Record (and may...

In the block below, you need to assign the team to batting. batting.teams = {your_team_object} Then you will be able to access the team information. You can create another fetch to Core Data using the teamID to retrieve the team object. for (id b in BATTING_UVWXYZ) { Batting *batting =...

Tests require configuration. Generally you should assume that if you don't explicitly give a test some information then it doesn't have any information. So, you need to give the test a context to work with (and potentially a data source in your specific case). test.managedObjectContext = ... ...

Your backtrace says it all, you are setting up too many connections to the persistent store. The MagicalRecord documentation details that you should set up your stack either in the App Delegate's didFinishLaunching phase, or in your case, when your DatabaseManager class is instantiated. So the call to MagicalRecord.setupCoreDataStackWithStoreNamed("Records") should...

A far better solution is to design the application so that this is not necessary. You are effectively blocking the entire application (including the UI thread) from doing anything while a migration is happening. Better to develop this as a state engine, do the migration in the background and then...

I think it not require two MOC, you are able to solve it by one. Just pass it as a parameter to the fetchOtherMOByMyMo:(MyMo *)mo onContext:(NSManagedObjectContext *)context. And you forget to use performBlock: Check, it should work without crashing: - (void)massDelete { ... __weak typeof(self) weakSelf = self; self.managedObjectContext performBlock:^{ NSArray...

You can't directly pass managed objects between contexts. Each NSManagedObject can only be accessed by its own context. You'll need to pass its objectID to the completion block, then have the main context fetch the object by calling one of the following methods: -(NSManagedObject *)objectWithID:(NSManagedObjectID *)objectID This will create a...

[managedObjectContext executeFetchRequest:fetchRequest error:nil] returns an (immutable) NSArray, and mutableCopy creates a - well - mutable copy of that array. It does not copy the managed objects in the array or the context. It just allows you to modify self.notes, e.g. to add, delete or rearrange the objects in the mutable...

There are a number of places where your problem could lie. I'll list them all, in the hope that one of them is of some help: First, in your cellForRowAtIndexPath method, you have: NSManagedObject *device = [self.allreadArray objectAtIndex:indexPath.section]; Hence you are using the section number to determine which device you...

Quick answer to reflect comments. If the MOC goes out of scope, then it makes sense the entity instance pointers that you've retained have internal pointers to a MOC that's now nil. Beyond that, I don't know what is supposed to happen with the usability or mutability of these NSManagedObjects...

If I then blindly follow the tutorials Never a good idea, you need to take what you've learnt from the tutorials and apply it to your problem space. The items are indeed persisted as you're using Core Data. You don't need to but it does help with memory management....

No, you can create instances of NSManagedObject subclasses and add them to the managed object context later (while I'd suggest not do do so). You have some issue with your Sketch object, not with NSManagedObject and NSManagedObjectContext. The only thing is that you should create it like this: NSManagedObjectContext *moc...

NSAssert does not normally run in a Release Build as NS_BLOCK_ASSERTIONS is defined as part of the standard Xcode template. Or from the docs IMPORTANT Do not call functions with side effects in the condition parameter of this macro. The condition parameter is not evaluated when assertions are disabled, so...

After looking into all the suggestions I could find for similar problems, none of them seemed to help. The scenario i was describing in the end had the store stuff to file handled ok, but the UI not updating. At the time when I do this import/export I'm in a...

NSUndoManager is not really suited to this task. You can tell it to undo or redo actions, but you can't inspect those actions or selectively save data in the current undo stack. What I've done in the past is create my own queue of outgoing changes. Whenever changes are saved...

You need to create a wrapper class that would be instantiated using core data entity and work with objects of that class in your code. For example, if you have such entity @interface Item: NSManagedObject NSInteger id; NSString *name; @end You should create a class @interface ItemObject: NSObject NSInteger itemId;...

You can't do [[Note alloc] init] because Note is an NSManagedObject subclass. The designated initializer is awakeFromInsert: which is called after you insert the Note object in a managedObjectContext. That gets called if you do something like this: self.note = [Note [NSEntityDescription insertNewObjectForEntityForName:@"Note" inManagedObjectContext:self.managedObjectContext]; See "Creating and Deleting Managed Objects"...

You can use temporaryID property of NSManagedObjectID: YES if the receiver is temporary, otherwise NO. Most object IDs return NO. New objects inserted into a managed object context are assigned a temporary ID which is replaced with a permanent one once the object gets saved to a persistent store. Example...

The docs are ambiguous at best. In my testing, you do not need to use a queue to attach the NSManagedObjectContext to its parent or the NSPersistentStoreCoordinator. If you are doing a NSConfinmentConcurrencyType I set the parent or coordinator on the thread that created it (since that is the thread...

The way I normally do this is to give the cell a property describing what to display and override setProperty in the cell to reconfigure it based on the new data. You anyway have to do something similar for the recycling table view cells, so performance should not be an...

I found this but it was not working. By the way it is something wrong with compilation. I removed model file, added a new empty model with previous naming but now from Xcode. And then edited the model file manually, and inserted the relating xml tags.

Is it safe to initialize NSManagedContext in one thread and then pass it to another thread where you do inserts/fetches inside performBlock:? It's safe if you do everything that touches Core Data inside a performBlock: call. Inserts and fetches, sure. But also any time you touch a managed object...

The issue is that two objects have to belong to the same context in order to establish relationships between them. In your case, one of the contexts has a private type, it has its own queue to work at, where the main context works on the main queue. This is...

To obtain an NSManagedObjectID from the URI representation, you must call the appropriate method on the NSManagedObjectContext or NSPersistentStoreCoordinator - managedObjectIDForURIRepresentation:. The URI representation includes the UUID of the store used to persist the object. Calling managedObjectIDForURIRepresentation: will cause Core Data to attempt to find the correct store based on...

Rather than having 2 separate entities for your ToDoList, I would have one entity with an isDone Boolean attribute. Cells on your ToDoTVC would display objects where isDone is false and Cells on your DoneTVC would display objects where isDone is true. When you create your ToDoListItem, set its initial...

Answers: Yes, you can simply delete the data store. (Check if it exists with NSFileManager.) Yes, you can simply use another data model. No, refactoring an old data model sounds like pain without any tangible benefits. As mentioned in the comment by Mike, the only concern is if this is...

There are two approaches to setting the context: Calling back to the App Delegate:, like this let appDelegate : AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate let context = appDelegate.managedObjectContext! or passing the context forward from the App Delegate to the Master View Controller, which then passes it on to any subsequent...

Apple sample code stores and creates the Core Data stack in the app delegate, but that doesn't mean it's right. In fact, it's entirely wrong. The app delegate shouldn't own the data store. Apple does it, and most people follow along, because it's convenient. You should really have a custom...

The general rule is that you should always use performBlock: or performBlockAndWait: when doing any operation involving that context, including just reading objects. The only exceptions are main queue contexts (where you can use performBlock: if you wish, but there's no requirement to if you're on the main thread), and...

It depends on what you want to do really! If you want to display a list of objects of the same entity with a filter or order then yes its great for that. If you want to have a form where attribute values of an entity object are change then...

You are on the right track. The best lever I have found for optimizing Core Data save times is indeed finding the right batch size. As suggested by one commenter, the best way is to test it out. There are no definite rules because it really depends on the nature...

It does not seem to make too much sense to create a child context and then fetch from the parent context. I do not believe that this is the way child contexts' blocks were conceived to be used. To clear up the confusion: after creating the child context from a...

That type of merge strategy is something you need to deal with and is outside of the scope of the framework. Basically you have a dirty sandbox and a clean sandbox. When a change is made in the clean sandbox it will get propagated to the dirty one. It is...

If you look at NSFetchedResultsController documentation, there is a section about "Handling Object Invalidation" which states the following, When a managed object context notifies the fetched results controller that individual objects are invalidated, the controller treats these as deleted objects and sends the proper delegate calls. It’s possible for all...

I was able to reproduce your error message. You get this message because you didn't set correctly your entity class name in the Core Data Model Editor. Define your entity class name fields in the Core Data Model Editor as <MyAppName>.Employee and <MyAppName>.Contact. See this previous answer for more details....

What is your goal with createPostInFeed? You show that you are doing a fetch but what do you do with that fetch? Is this a "insert or update" check? or is it just a "insert or skip" test? Any fetch is going to lock the NSPersistentStoreCoordinator and cause your application...

one MOC per thread is recommended. There are exceptions but that general rule still holds. Don't create NSThread objects. Just don't. Too much pain. Instead use blocks or NSOperation instances. They are easier to grok and protect you from a lot of pain. Do not use locks with Core...

First, check that you filled the class in the data model: As ProjectName.AddrBook (for swift classes you have to specify even the project name). (NOTE: this is needed only if you haven't used the prefix @objc(AddrBook) before the class, but I see that you used it, so this is not...