天道酬勤,学无止境

nsmanagedobjectcontext

核心数据:我应该从父上下文中获取对象还是子上下文与父上下文具有相同的对象?(Core Data: should I be fetching objects from the parent context or does the child context have the same objects as the parent?)

问题 我对ManagedObjectContext父/子上下文有些困惑。 当我设置子上下文并设置父上下文时,子上下文是否包含父上下文的所有对象? 我正在使用在AppDelegate创建的股票Core Data方法,但我将ConcurrencyQueue更改为 main。 在我应该更新数据库的方法中: 创建子上下文,设置父上下文在子上下文上执行块从父上下文获取在子上下文中创建或更新对象在子上下文中调用保存有通知侦听器来处理子上下文保存保存父上下文 我的问题是看起来我没有将任何内容保存到子上下文中。 我没有收到更新或创建 ChatMessage 的 println 消息。 我在这里做错了什么? AppDelegate 核心数据方法 lazy var managedObjectContext: NSManagedObjectContext? = { // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could

2021-09-20 20:01:27    分类:技术分享    ios   swift   core-data   nsmanagedobjectcontext   managedobjectcontext

如何在 Swift 中正确测试核心数据(How to test Core Data properly in Swift)

问题 已经有很多关于此的主题,但我还没有找到适用于 Swift(Xcode 6.2)的解决方案。 为了在 Swift 中测试 Core Data 支持的类,我生成了新的托管对象上下文,然后将其注入到我的类中。 //Given let testManagedObjectContext = CoreDataTestComposer.setUpInMemoryManagedObjectContext() let testItems = createFixtureData(testManagedObjectContext) as [TestItem] self.itemDateCoordinator.managedObjectContext = testManagedObjectContext //When let data = self.itemDateCoordinator.do() //Then XCTAssert(data.exists) 问题来自将测试中创建的 MOC 传递给正在执行的类。 因为实体类是命名空间的,Core Data 不会获取您适当的 ManagedObject 子类,而是交回一个NSManagedObject集。 当循环或对这些对象执行任何操作时(在您的类中将是一组测试项( [TestItem] )。 例如

2021-09-20 02:02:18    分类:技术分享    ios   swift   core-data   nsmanagedobjectcontext

iOS 中的 NSOperation - 如何处理循环和嵌套的 NSOperation 调用以获取图像(NSOperation in iOS - How to handle loops and nested NSOperation call to get Images)

问题 我需要帮助了解如何适当处理以下用例: 假设我正在编写一个聊天应用程序: 启动应用程序请求服务器 ( AFHTTPRequestOperation ) 给我一个所有新消息的列表循环浏览这些消息,看看我是否需要下载任何图像如果是,则再次调用服务器( AFImageRequestOperation )以获取图像 当我的托管对象“消息”不再处于同一上下文中时,我不断遇到崩溃,但我只使用了一个managedObjectContext 。 是不是因为我嵌套调用的方式,因为它们是异步的? 我几乎肯定该消息不会在其他地方被删除,因为我看到了。 需要注意的一件事是,当我更改视图控制器时似乎会发生这种情况。 我启动应用程序,并在根视图控制器 (RVC) 中执行上面的步骤 #2。 如果我在下载所有图像之前触摸“ MessageListViewController ”(MLVC),则那些未完成下载的图像的关联消息突然managedObjectContext nil managedObjectContext 。 下面是相关代码: AFHTTPRequestOperation * operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:requestImageInfoURL success:^(NSURLRequest

2021-09-13 04:05:54    分类:技术分享    ios   asynchronous   nsmanagedobject   nsmanagedobjectcontext   nsoperation

NSmanaged 上下文线程(NSmanaged context threads)

问题 我使用单例来处理数组等。跨越应用程序中的视图。 要初始化单例和NSManagedObjectContext ,以便我可以获取对象,我使用: +(DataControllerSingleton *)singleDataController { static DataControllerSingleton * single=nil; @synchronized(self) { if(!single) { single = [[DataControllerSingleton alloc] init]; NSManagedObjectContext *context = [single.fetchedResultsController managedObjectContext]; single.masterCareList = [[NSMutableArray alloc] init]; } } return single; } 当我插入一个新对象时,该对象将不会出现在显示功能中,直到我重新启动应用程序。 我通过这个方法在单例类中插入新对象: - (void)insertNewObject:(Care *)care { NSManagedObjectContext *context = [self.fetchedResultsController

2021-09-12 16:20:42    分类:技术分享    ios   core-data   singleton   nsmanagedobjectcontext

为什么未保存的 managedObject 会丢失其 managedObjectContext(Why does an unsaved managedObject lose its managedObjectContext)

问题 目前,我正在开发一个应用程序来预订汽车。 所有与预订相关的数据都存储在实体“预订”中。 由于“预订”的某些属性或“预订”与其他实体之间的关系是强制性的,我决定将实体“预订”的所有托管对象添加到它们自己的托管对象上下文中。 这个上下文也将存储在一个单独的变量中以避免丢失它。 这很好用,除非我会签署(企业商店或临时)我的应用程序并部署它。 ARC 已启用。 课程预订界面 @interface Bookings : NSManagedObject { @private NSManagedObjectContext *mContext; } @end 课程预订实施 @implementation Bookings { + (Bookings *)booking { NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:concurrencyType]; [context setPersistentStoreCoordinator:[self persistentStoreCoordinator]]; Bookings *object = (Bookings*)[[NSManagedObject alloc] initWithEntity:[self

2021-09-12 09:18:19    分类:技术分享    core-data   nsmanagedobject   nsmanagedobjectcontext

NSmanaged context threads

I use a singleton for working with arrays etc. cross the views in the application. To initialize the singleton and the NSManagedObjectContext, so that I can fetch objects, I use: +(DataControllerSingleton *)singleDataController { static DataControllerSingleton * single=nil; @synchronized(self) { if(!single) { single = [[DataControllerSingleton alloc] init]; NSManagedObjectContext *context = [single.fetchedResultsController managedObjectContext]; single.masterCareList = [[NSMutableArray alloc] init]; } } return single; } When I insert a new object that object will not show up in display

2021-09-08 19:33:50    分类:问答    ios   core-data   singleton   nsmanagedobjectcontext

How to synchronize two independent NSManagedObjectContext in Core Data?

I have two NSManagedObjectContext which are independent: there is no relationship between the two. How to synchronize the two in Core Data?

2021-09-08 04:21:18    分类:问答    ios   core-data   nsmanagedobjectcontext

Why does an unsaved managedObject lose its managedObjectContext

Currently, I am developing an app to book cars. All booking related data are stored in an entity 'Bookings'. As some attributes of 'Bookings' or relationships between 'Bookings' and other enties are mandatory I decided to add all managedObjects of entity 'Bookings' to their own managedObjectContext. This context will also be stored in a separate variable to avoid losing it. This works fine unless I'll sign (enterprise store or adhoc) my app and deploy it. ARC is enabled. Class Bookings interface @interface Bookings : NSManagedObject { @private NSManagedObjectContext *mContext; } @end Class

2021-09-06 15:08:09    分类:问答    core-data   nsmanagedobject   nsmanagedobjectcontext

NSOperation in iOS - How to handle loops and nested NSOperation call to get Images

I need help understanding how to appropriate handle the following use case: Say I'm writing a Chat app: Launch App Ask server (AFHTTPRequestOperation) to give me a list of all new messages Loop through those messages to see if I need to download any images If yes, then make another call to the server (AFImageRequestOperation) to get the image I keep getting crashes where my managed object "message" is no longer in the same context, but I'm only using one managedObjectContext. Is it because of the way I'm nesting the calls, since they are asynchronous? I am almost positive the message isn't

2021-09-05 15:28:33    分类:问答    ios   asynchronous   nsmanagedobject   nsmanagedobjectcontext   nsoperation

How should I guarantee fetch results from a different thread in a nested contexts are up to date, when saves are done asynchronously in background?

I've read the following Behavior differences between performBlock: and performBlockAndWait:? But wasn't able to find an answer to my question. The following code is picked up from an RayWenderlich video. Specifically at 10:05 the code is something like this: class CoreDataStack { var coordinator : NSPersistentStoreCoordinator init(coordinator: NSPersistentStoreCoordinator){ self.coordinator = coordinator } // private, parent, in background used for saving private lazy var savingContext : NSManagedObjectContext = { let moc = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)

2021-09-04 15:16:58    分类:问答    swift   core-data   concurrency   synchronization   nsmanagedobjectcontext