无法使用MagicalRecord删除源自RestKit的NSManagedObject(Cannot delete NSManagedObject originated in RestKit wit

系统教程 行业动态 更新时间:2024-06-14 16:57:17
无法使用MagicalRecord删除源自RestKit的NSManagedObject(Cannot delete NSManagedObject originated in RestKit with MagicalRecord)

我的用例很简单。 我需要

创建一个请求 NSManagedObject,✓工作 将它传递给例如来自Restkit的postObject:方法,✓有效 在完成块中收到响应 NSManagedObject,✓工作 处理它,✓工作 使用MR_deleteEntity删除请求和响应对象,✘不起作用

我想使用MagicalRecord来创建/删除/管理实体。

问题:

当我从MagicalRecord工具包调用异步保存方法时,在我退出应用程序后,我仍然可以看到sqlite db文件中的实体。 重新启动应用程序后,尽管我在对象上显式调用MR_deleteEntity,但在db中添加新对象而不删除单个实例。 我承认上下文管理是我还没有完全掌握的。

我使用以下设置将MagicalRecord与Restkit连接:

// 1. Setup the core data stack with the automigration [MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:[GVUserDefaults standardUserDefaults].applicationStoreName]; // 2. Initialize managed object store RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithPersistentStoreCoordinator:[NSPersistentStoreCoordinator MR_defaultStoreCoordinator]]; // 3. create the persistentStoreManagedObjectContext and the mainQueueManagedObjectContext: [managedObjectStore createManagedObjectContexts]; // 4. set the default and the root saving context: [NSManagedObjectContext MR_defaultStoreCoordinator:managedObjectStore.mainQueueManagedObjectContext]; [NSManagedObjectContext MR_setRootSavingContext:managedObjectStore.persistentStoreManagedObjectContext]; // 5. create RestKit manager: self.rkManager = [TSNRKObjectManager managerWithBaseURL:[NSURL URLWithString:[self serverURL]]]; self.rkManager.requestSerializationMIMEType = RKMIMETypeJSON; self.rkManager.managedObjectStore = managedObjectStore;

我试图以这种方式删除请求响应对象:

[self saveWithBlock:^(NSManagedObjectContext *localContext) { // calls [MagicalRecord saveWithBlock:block completion:completion] // some additional processing, getting data from the response [loginResponse MR_deleteEntity]; [loginRequest MR_deleteEntity]; } completion:^(BOOL success, NSError *error) { // some additional processing }];

但我总是在日志中收到此消息:

-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x110906470) NO CHANGES IN ** UNNAMED ** CONTEXT - NOT SAVING

我使用此页面作为参考https://gist.github.com/tonyarnold/4694673 。 创建请求和响应实体的上下文是一样的吗? Restkit用于创建实体的上下文是什么? 我是否也应该在[MagicalRecord saveWithBlock:block completion:completion]中的块内创建请求实体? https://github.com/blakewatters/RKMagicalRecord中的示例不包括自动迁移设置和异步保存方法。

更新:

这是可接受的解决方案吗? (我的意思是干净,优雅的删除):

[self.loginRequest MR_deleteEntity]; [self.loginRequest.managedObjectContext MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) { // log }]; [self.loginResponse MR_deleteEntity]; [self.loginResponse.managedObjectContext MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) { // log }];

我测试了这个,它的工作原理。

更新2

在从Restkit的postObject: ...调用触发的完成块中,我可以通过这种方式清除持久存储中的实体:

[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) { [[self.loginRequest MR_inContext:localContext] MR_deleteEntity]; [[self.loginResponse MR_inContext:localContext] MR_deleteEntity]; } completion:^(BOOL success, NSError *error) { if(success) { self.loginRequest = nil; self.loginResponse = nil; // log, update ui } else { // log error } }];

my use case is very simple. I need to

create a request NSManagedObject, ✓ works pass it to e.g. postObject: method from Restkit, ✓ works receive a response NSManagedObject in the completion block, ✓ works process it and, ✓ works delete both the request and the response objects using MR_deleteEntity, ✘ does not work

I'd like to use just MagicalRecord to create/delete/manage the entities.

The issue:

When I call the asynchronous save method from the MagicalRecord toolkit and after I exit the app I can still see the entities in the sqlite db file. After restart of the app new objects are added in the db without deleting a single instance although I explicitly call MR_deleteEntity on the objects. I admit the context management is something I yet have not grasped fully.

I am using the following setup to connect MagicalRecord with Restkit:

// 1. Setup the core data stack with the automigration [MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:[GVUserDefaults standardUserDefaults].applicationStoreName]; // 2. Initialize managed object store RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithPersistentStoreCoordinator:[NSPersistentStoreCoordinator MR_defaultStoreCoordinator]]; // 3. create the persistentStoreManagedObjectContext and the mainQueueManagedObjectContext: [managedObjectStore createManagedObjectContexts]; // 4. set the default and the root saving context: [NSManagedObjectContext MR_defaultStoreCoordinator:managedObjectStore.mainQueueManagedObjectContext]; [NSManagedObjectContext MR_setRootSavingContext:managedObjectStore.persistentStoreManagedObjectContext]; // 5. create RestKit manager: self.rkManager = [TSNRKObjectManager managerWithBaseURL:[NSURL URLWithString:[self serverURL]]]; self.rkManager.requestSerializationMIMEType = RKMIMETypeJSON; self.rkManager.managedObjectStore = managedObjectStore;

Question

I am trying to delete the request and the response objects this way:

[self saveWithBlock:^(NSManagedObjectContext *localContext) { // calls [MagicalRecord saveWithBlock:block completion:completion] // some additional processing, getting data from the response [loginResponse MR_deleteEntity]; [loginRequest MR_deleteEntity]; } completion:^(BOOL success, NSError *error) { // some additional processing }];

But I always get this message in the log:

-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x110906470) NO CHANGES IN ** UNNAMED ** CONTEXT - NOT SAVING

I am using this page as reference https://gist.github.com/tonyarnold/4694673. The contexts for the creation of the request and the response entity are the same? What context is Restkit using for creation of the entities? Should I create the request entity also within the block in [MagicalRecord saveWithBlock:block completion:completion]? The example from https://github.com/blakewatters/RKMagicalRecord does not include the automigration setup and the asynchronous saving methods.

UPDATE:

Is this an acceptable solution? (I mean clean, graceful deletion):

[self.loginRequest MR_deleteEntity]; [self.loginRequest.managedObjectContext MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) { // log }]; [self.loginResponse MR_deleteEntity]; [self.loginResponse.managedObjectContext MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) { // log }];

I have tested this and it works.

UPDATE 2

In the completion block triggered from Restkit's postObject:... call, I can cleanup the entities from the persistent store this way:

[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) { [[self.loginRequest MR_inContext:localContext] MR_deleteEntity]; [[self.loginResponse MR_inContext:localContext] MR_deleteEntity]; } completion:^(BOOL success, NSError *error) { if(success) { self.loginRequest = nil; self.loginResponse = nil; // log, update ui } else { // log error } }];

最满意答案

您误解了多个上下文的保存语义。 当您调用saveWithBlock:您需要将所有托管对象传输到在您操作之前为您创建的新本地上下文,否则上下文没有更改,也不会保存。 要传输,您需要获取托管对象ID并找到existingObjectWithID:error: .

在这种情况下,对于2个对象删除,最好直接从主上下文(它们属于它们)中删除对象,并将其保存到持久性存储中。

You misunderstand the saving semantics for your multiple contexts. When you call saveWithBlock: you need to transfer all of your managed objects to that new local context that is created for you before you operate on them, otherwise the context has no changes and isn't saved. To transfer you need to get the managed object ids and find the existingObjectWithID:error:.

In this case, for 2 object deletion a you are better off deleting the objects directly from the main context (which is the one they belong to) and saving it up to the persistent store.

更多推荐

本文发布于:2023-04-12 19:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/3687929c6b29a546fbe31cab12fe0a05.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:RestKit   MagicalRecord   NSManagedObject   originated   delete

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!