nsmanagedobjectcontext

Singleton managedObjectContext

与世无争的帅哥 提交于 2019-12-19 04:05:39
问题 I want to use the singleton UIApplication to access the managedObjectContext of the AppDelegate. But when I write [[[UIApplication sharedApplication] delegate] managedObjectContext] or [[[UIApplication sharedApplication] delegate] __managedObjectContext] it doesn't work. But this line works fine : NSLog(@"Seeking for the AppDelegate : %@", [[[UIApplication sharedApplication] delegate] class]); Do you have a solution ? Niels 回答1: Try casting it to your actual app delegate implementation, like

Core data multithreading fetch record

北战南征 提交于 2019-12-18 18:04:22
问题 I have one doubt about multithreading in coredata. if we are using multithreading we should use separate NSManagedObjectContext for inserting new data or updating or else we can use parent child context method. But i am creating new NSManagedObjectContext only. My question is that should i use separate NSManagedObjectContext for even fetching in background thread also. If not (i.e. we can use main queue NSManagedObjectContext only) than why i m getting __psynch_mutexwait error. Thanks, 回答1:

Core data find-or-create most efficient way

给你一囗甜甜゛ 提交于 2019-12-18 15:34:53
问题 I have around 10000 objects of entity ' Message '. When I add a new ' Message ' i want to first see whether it exists - and if it does just update it's data, but if it doesn't to create it. Right now the "find-or-create" algorithm works with by saving all of the Message objects ' objectID ' in one array and then filtering through them and getting the messages with existingObjectWithID:error: This works fine but in my case when I fetch an 'Message' using existingObjectWithID : and then try to

what's the difference between NSManagedObjectContext reset and rollback?

谁都会走 提交于 2019-12-18 14:14:33
问题 The documentation says: - (void)reset Returns the receiver to its base state. Discussion All the receiver's managed objects are “forgotten.” If you use this method, you should ensure that you also discard references to any managed objects fetched using the receiver, since they will be invalid afterwards. - (void)rollback Removes everything from the undo stack, discards all insertions and deletions, and restores updated objects to their last committed values. Discussion This method does not

how do you create a NSManagedObjectContext

半世苍凉 提交于 2019-12-18 13:30:50
问题 In core data for the iPhone, I was getting all sorts of errors trying to save data to a NSManagedObjectContext . I believe that my issues were all to do with me using a NSManagedObjectContext that was being used in multiple threads. So I wanted to create a new NSManagedObjectContext and try that, but I cannot find the example code to simply create a new instance... I know its simple, but I would really appreciate any help here. Note, I have seen this article on the Apple docs: http:/

Core Data Fault

我的未来我决定 提交于 2019-12-17 19:49:37
问题 I am mapping Json Data from Server using Restkit and I am Displaying those data by fetching from db. There is a refresh button in my view which performs the above operation again. Scenario: I have two tables Key & Profile which has one-one relationship. I am fetching data from DB using follwing code NSFetchRequest *fetchRequest = [Key fetchRequest]; [fetchRequest setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObject:@"Profile"]]; [fetchRequest setIncludesSubentities:YES]; NSArray

Correct implementation of parent/child NSManagedObjectContext

末鹿安然 提交于 2019-12-17 07:13:44
问题 My app sometimes inserts objects into the managed object context that are not meant to necessarily be saved. For example, when I launch an 'add entity' modal, I create a managed object and assign it to the modal. If the user saves from that modal, I save the context. If he cancels, I delete the object and no save is necessary. I have now introduced an 'import' feature that switches to my app (using a URL scheme) and adds an entity. Because one of these modals might be open, it is not safe to

Illegal access to managed object context in main thread, why?

流过昼夜 提交于 2019-12-13 18:55:37
问题 I've enabled com.apple.CoreData.ConcurrencyDebug 1 to check Core Data concurrency errors. I have the following code snippet in a Swift class: lazy var context: NSManagedObjectContext! = { var appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate return appDelegate.managedObjectContext! }() func getAllEntitiesToRootContext() -> [MyEntity]? { let fetchRequest = NSFetchRequest(entityName:"MyEntity") do { let fetchedResults = try context.executeFetchRequest(fetchRequest) as!

How to synchronize two independent NSManagedObjectContext in Core Data?

。_饼干妹妹 提交于 2019-12-13 17:18:15
问题 I have two NSManagedObjectContext which are independent: there is no relationship between the two. How to synchronize the two in Core Data? 回答1: Yes you can do that if you are having two context like on is in background thread and other is on main thread for example then you can use mergeChangesFromContextDidSaveNotification to synchronize the data of two managed object context // MARK: - // MARK: Notification Handling func managedObjectContextDidSave(notification: NSNotification) { dispatch

Refresh NSPersistentStoreCoordinator in AppDelegate from ViewController

戏子无情 提交于 2019-12-13 07:36:05
问题 I have an app where the PersistentStoreCoordinator is set up within AppDelegate. I want to load a new sqlite database from a list of saved sqlite files. I'm happy with deleting the existing Persistent Store and sqlite file and replacing the sqlite file with the one loaded from the table. However, how do I reload/refresh the PersistentStoreCoordinator from the ViewController action of selecting the new file? I've tried setting AppDelegate as a delegate of the ViewController, but this seems to