nsmanagedobjectcontext

Why doesn't NSManagedObject instances hold a strong reference to their NSManagedObjectContext?

試著忘記壹切 提交于 2019-11-27 16:28:08
问题 As pointed out in another question on SO (and the Apple docs), NSManagedObject instances do not hold a strong reference to the NSManagedObjectContext from which they originated. On first blush, this seems like a strange decision, since NSManagedObject instances are nearly useless without their context , since it leads to confusing bugs such as faults not firing when they should. Can anyone provide some background on why this is the case? Would it be dangerous to implement an NSManagedObject

Fetching selected attribute in entities

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 14:55:21
I have a core-data entity with several attributes, and I want a list of all the objects in one attribute. My code looks like this: let appDel:AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate let context:NSManagedObjectContext = appDel.managedObjectContext! let sortDesc = NSSortDescriptor(key: "username", ascending: true) let fetchReq = NSFetchRequest(entityName: "Identities") fetchReq.sortDescriptors = [sortDesc] fetchReq.valueForKey("username") let en = NSEntityDescription.entityForName("Identities", inManagedObjectContext: context) userList = context

How do I copy or move an NSManagedObject from one context to another?

試著忘記壹切 提交于 2019-11-27 10:14:44
I have what I assume is a fairly standard setup, with one scratchpad MOC which is never saved (containing a bunch of objects downloaded from the web) and another permanent MOC which persists objects. When the user selects an object from scratchMOC to add to her library, I want to either 1) remove the object from scratchMOC and insert into permanentMOC, or 2) copy the object into permanentMOC. The Core Data FAQ says I can copy an object like this: NSManagedObjectID *objectID = [managedObject objectID]; NSManagedObject *copy = [context2 objectWithID:objectID]; (In this case, context2 would be

Behavior differences between performBlock: and performBlockAndWait:?

那年仲夏 提交于 2019-11-27 09:32:45
问题 I'm creating an NSManagedObjectContext in a private queue to handle data updates I take from files and/or services: NSManagedObjectContext *privateContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; privateContext.persistentStoreCoordinator = appDelegate.persistentStoreCoordinator; Since I'm using a private queue, I don't fully understand the difference between performBlock:

Core Data merge two Managed Object Context

余生长醉 提交于 2019-11-27 09:29:59
问题 My Cocoa/Application has a Managed Object Context on the main thread. When I need to update my data my program will: Start a new thread Receive new data from a server Create a new Managed Object Context Send a notification to the main thread in order to merge the two context This is the function that receive the notification on the main thread - (void)loadManagedObjectFromNotification:(NSNotification *)saveNotification { if ([NSThread isMainThread]) { [self.managedObjectContext

Implementing Fast and Efficient Core Data Import on iOS 5

你。 提交于 2019-11-27 05:46:01
Question : How do I get my child context to see changes persisted on the parent context so that they trigger my NSFetchedResultsController to update the UI? Here's the setup: You've got an app that downloads and adds lots of XML data (about 2 million records, each roughly the size of a normal paragraph of text) The .sqlite file becomes about 500 MB in size. Adding this content into Core Data takes time, but you want the user to be able to use the app while the data loads into the data store incrementally. It's got to be invisible and imperceptible to the user that large amounts of data are

Correct implementation of parent/child NSManagedObjectContext

為{幸葍}努か 提交于 2019-11-27 03:27:53
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 save the context at this point. The transient object created for the modal will be saved, even if the

Cross-model relationships in NSManagedObjectModel from merged models?

删除回忆录丶 提交于 2019-11-27 02:20:16
问题 Is it possible to model relationships between entities that are defined in separate NSManagedObjectModels if the entities are always used within an NSManagedObjectModel that is created by merging the relevant models? For example, say model 1 defines an entity Foo with relationship (one-to-one) toBar and that model 2 defines an entity Bar with a relationship (one-to-one) toFoo . I will build a CoreData stack using -[NSManagedObjectModel mergedModelFromModels] , merging model 1 and model 2. Is

Core Data: Do child contexts ever get permanent objectIDs for newly inserted objects?

血红的双手。 提交于 2019-11-27 00:38:54
I have an app with two managed object contexts setup like this: Parent Context: NSPrivateQueueConcurrencyType, linked to the persistent store. Main Context: NSMainQueueConcurrencyType, child of Parent Context. When insert a new managed object to the main context, I save the main context and then the parent context like this: [context performBlockAndWait:^{ NSError * error = nil; if (![context save: &error]) { NSLog(@"Core Data save error %@, %@", error, [error userInfo]); } }]; [parentContext performBlock:^{ NSError *error = nil; BOOL result = [parentContext save: &error]; if ( ! result ) {

What is NSManagedObjectContext's performBlock: used for?

不打扰是莪最后的温柔 提交于 2019-11-26 23:48:57
问题 In iOS 5, NSManagedObjectContext has a couple of new methods, performBlock: and performBlockAndWait: . What are these methods actually used for? What do they replace in older versions? What kind of blocks are supposed to be passed to them? How do I decide which to use? If anyone has some examples of their use it would be great. 回答1: The methods performBlock: and performBlockAndWait: are used to send messages to your NSManagedObjectContext instance if the MOC was initialized using