How to Completely Reset RestKit

793 views
Skip to first unread message

Mike

unread,
Aug 4, 2011, 8:28:16 AM8/4/11
to RestKit
Hello List!

I have been slamming my head against this problem for a couple weeks
now without any luck. I'm hoping someone could give me a suggestion on
what I might be doing wrong.

My CoreData backed RestKit app has a "reset" button. This button wipes
out everything from the local database, and resets the main
application's object model, managed object context, etc.

The wiping part works just fine, I get a fresh local database with no
data in it, and my app restarts and runs as usual. However, I hit a
problem when I make my first object mapping via RestKit. It seems that
RestKit can't save the data it pulls down from the server to the local
database. I'm guessing that there is an old reference hanging around
inside RestKit after my reset, but I can't figure out where.

My code to reset the app works like this:

* release all local references to core data objects

* release the main managedObjectContext and managedObjectModel of the
app

* delete the actual sqlite database file from the file system

* attempt to reset/release the RestKit managedObjectContext like this:
[RKObjectManager setSharedManager:nil];

* create a new db and re-instantiate the MOM, MOC, initialize RestKit,
etc.


Once I reset, I try a RestKit request, and see the following error:

2011-08-04 14:13:57.933 MyApp[34590:8f93] E
restkit.network:RKObjectLoader.m:186 Encountered errors during
mapping: The operation couldn’t be completed. (Cocoa error 1550.), The
operation couldn’t be completed. (Cocoa error 1550.), The operation
couldn’t be completed. (Cocoa error 1550.)
2011-08-04 14:13:57.934 MyApp[34590:8f93] E
restkit:RKManagedObjectLoader.m:129 Failed to save managed object
context after mapping completed: The operation couldn’t be completed.
(Cocoa error 1560.)

I believe Cocoa error 1560 has something to do with a bad reference to
a ManagedObjectContext.

Can anyone suggest something else I can do to ensure that I fully
reset RestKit when I reset CoreData on my app?

Mike

unread,
Aug 4, 2011, 9:03:55 AM8/4/11
to RestKit
Of course shortly after posting this, I think I answered my own
question.

In case anyone else is wondering, what I did was instead of deleting
the local sqlite file myself, I call the following RestKit method:

[[[RKObjectManager sharedManager] objectStore] deletePersistantStore];

This both deletes the sqlite file, and resets the entire
persistentStoreCoordinator and MOC for RestKit. It seems to have wiped
out whatever stale context or object reference was breaking RK's
object mapping/saving to CoreData.

Very happy I finally figured it out, and it was already implemented in
RestKit! Sorry to be a bother.

Gregory Combs

unread,
Aug 5, 2011, 12:11:21 AM8/5/11
to res...@googlegroups.com
This is one I'd run into a while back, you answered your own post as I was thinking, "you're working to hard".  RestKit takes care of just about all of the issues with resetting the store, as you've discovered.

I've posted a wiki article on the matter:


Mike

unread,
Aug 22, 2011, 6:55:41 AM8/22/11
to RestKit
Hi Gregory,

Thanks for the wiki post. It turns out that my "Reset App" function
does exactly what you have posted there.

It definitely works well for reseting the core data stack and I'm not
having any zombie references anymore. However, using Instruments I can
see that after calling the reset function, there is almost zero
reduction in memory usage. It seems that resetting the app in this way
leaks quite a bit of memory.

Using the Leaks Instrument, I can see that nearly all the leaked
objects are from within RestKit. They seem mostly to be related to
object mapping: RKObjectMapping, RKObjectAttributeMapping,
RKObjectMappingProvider.

I'm wondering what more can be done to release the remaining memory
held by RestKit. Should I be keeping local references to the
RKObjectMappings or the RKObjectMappingProvider so that I can release
them myself on reset?
> Reset-to-a-Factory-Fresh-Persistent-Store (GitHub)<https://github.com/RestKit/RestKit/wiki/Reset-to-a-Factory-Fresh-Pers...>

Gregory Combs

unread,
Aug 22, 2011, 3:30:40 PM8/22/11
to res...@googlegroups.com
Interesting. It may be as simple as telling the context to refault the objects. But I'm not sure why mappings would retain all those objects. I'll look into it if you post an issue on github with more details.

Are you running any other debug features besides leaks, like zombies? I know there's a couple that just force the app to ignore releases entirely.

Greg

Mike

unread,
Aug 22, 2011, 4:10:01 PM8/22/11
to RestKit
Greg,
Thanks for the reply.

I am running Zombies in the simulator, but I don't think that's what
is causing this as everything else in my app appears to be getting
released properly.

However, I have made some progress in reducing the amount of leakage
after resetting the app. After the DATA_STORE_RELOAD notification, I
added the following lines:

[[RKObjectManager sharedManager] setMappingProvider:nil];
[[RKObjectManager sharedManager] setObjectStore:nil];
[RKObjectManager setSharedManager:nil];

This seams to have cleaned out all of the RK-related objects that were
showing up in the leaks instrument after a reset.

Gregory Combs

unread,
Aug 22, 2011, 5:54:50 PM8/22/11
to res...@googlegroups.com
Try turning off zombies ... I forget, but I think running zombies at the same time as leaks is counterproductive and misleading in the results.

But in any event, after you set these to nil in your reset, you just reinstantiate them for the next go-around?

Michael Lawlor

unread,
Aug 22, 2011, 6:36:46 PM8/22/11
to res...@googlegroups.com
Yep, that's right. After the app resets itself I run through my full Restkit initialization routine again: setting up the mappings, object manager, object store etc. Figure that's the only way to be absolutely sure I'm starting from a clean slate.

I'll try disabling zombies though while running the leaks instrument to see if that helps or changes anything. Thanks for the advice.

Blake Watters

unread,
Aug 26, 2011, 8:50:53 AM8/26/11
to res...@googlegroups.com
There is also a shared singleton on the queue. If you run into more issues with tear down, look in RKSpecEnvironment.h/m The unit tests tear the world down constantly to ensure testing from a clean env.

-- 
Blake Watters
Sent with Sparrow

siddhesh mahadeshwar

unread,
Jun 23, 2014, 6:16:53 AM6/23/14
to res...@googlegroups.com
Hi even I am facing the same problem as you, I have removed local sqlite file and re created successfully. But then i don't know how link new store object to rest kit ? I tried every possible solutions, It will be very help full if you guys help me with this 
Reply all
Reply to author
Forward
0 new messages