Creating object graph in swift, Core Data or vanilla objects?
Creating object graph in swift, Core Data or vanilla objects?
- Subject: Creating object graph in swift, Core Data or vanilla objects?
- From: Glen Huang <email@hidden>
- Date: Wed, 21 Mar 2018 16:56:21 +0800
Hi,
I'm using Core Data as my model, but I don't want to actually persist anything.
Anytime a view controller is shown, I load json data from my server and
populate the store.
The problem is that objects in Core Data persist between view controllers, even
with in-memory store type. But when I load json data, I’d like to have a fresh
start. So it seems I need to remove existing objects first.
You might say I shouldn't use Core Data in this case, just use vanilla objects.
But Core Data solves lots of problems, like no need to worry about memory leak
for circular dependencies (my model is pretty complex, lots of many-to-many
relationships etc), NSFetchedResultsController, etc. And with vanilla objects,
when you have a many-to-many relationship, it’s seems it’s impossible to
prevent memory leak:
class Club {
var people: [Person]
}
class Person {
var name: String
var clubs: [Club]
}
Array in swift always create strong reference if I’m not wrong, and the two
arrays can create circular references.
If I go with Core Data, to easily delete objects and have a fresh start, one
solution involves carefully setting up delete rule between object models and
then deleting ones at the level I want, and hope it could cascade throughout,
but the problem is that this approach is very error prone. If I misconfigure
some delete rules, there won’t be any errors, still leaving some objects in the
store.
I wonder what’s the best way to create object graph in swift without the risk
of leaking memory, if Core Data is the way to go, is there any better way to
easily detect objects that I forgot to delete? Or is there a better way that I
can guarantee that the store is clean before importing json data?
Best,
Glen
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden