4

I am trying to sync an iphone app with a web server using two flags - synched and is_deleted as described here https://stackoverflow.com/a/5052208

When the user deletes an item in the app, I set the is_deleted flag to true. Now I want to make sure that object does not show up in my app again since it has been deleted (for example in a table view), but I need to keep the object to perform the sync. So here is the question:

What is the best way to exclude all objects with is_deleted flag from showing up in the app by default?

I can think of two options now:

  1. Make almost every single query in the app check if the item is_deleted - does not seem very efficient at all

  2. Find a more generic way to automatically exclude all is_deleted=YES objects from the app, for example by overriding awakeFromFetch or some other method. But I am not sure how to exclude the objects from the context but keep them in the database for my sync.

What is the best way to do this? Is #2 even possible?

Community
  • 1
  • 1
farofeiro
  • 185
  • 6

2 Answers2

2

You're searching with NSPredicate, right? Just have every syncable class implement a default predicate that checks for your is_deleted state, and append onto the predicate to specialize for further queries.

Also: don't name it is_deleted. Consider is_safe_deleted or safely_removed_and_not_called_is_deleted -- 'cause the first time you write [mo isDeleted] when you really meant [mo is_deleted], you're going to have a bad time.

greymouser
  • 3,133
  • 19
  • 22
  • Thanks. Having to add a predicate every time is what I am trying to avoid - also because it makes all the coredata-generated relations kinda useless (i.e. `user.messages` will return deleted messages). Good point on the name, I actually used archived instead but was referring to the other answer about sync. – farofeiro Mar 06 '14 at 19:14
  • That's exactly what a fetched property is, though -- an NSPredicate baked into the MOM for any entity. Core Data is there to help you design and develop your model ... your model includes "archived" items, so I'm not sure what else there is to do. (As an aside, I've developed something exactly like this myself.) – greymouser Mar 06 '14 at 19:44
1

I guess you are considering this for the case when the user wants to delete some objects but there is no internet connection, right? In this case I would suggest actually deleting those objects from the database and storing the info about those deleted objects in a different form. For instance you could have a separate entity named “XXDelayedOperation” which would have all the necessary info you need to create a NSURLRequest. This looks like a more efficient way of handling it.

dariaa
  • 6,285
  • 4
  • 42
  • 58
  • I would go this way too. You could either store identifiers for the deleted objects in an external list (e.g. plist), or you could even add an entity in your model to track deletions (e.g. DeletedObject). When an object gets deleted, you delete the object, but create a DeletedObject record with some identifier. Later, you inform the server, and delete the DeletedObject record. – Drew McCormack Mar 07 '14 at 15:40
  • That is the use case for most objects, except a few that the user will be able to restore. This looks clean so will probably go with this and use the fetched predicates suggested by @greymouser for the restorable objects.thanks! – farofeiro Mar 08 '14 at 01:22