-1

Im using Parse for server side. And I have a table view with list of Contacts object from Parse. If user taps on object it saves it to parse and if taps again it deletes it from parse.

For saving I use method:

- (void)addContact:(Contact *)contact withBlock:(void (^)(void))completion {
    [contact saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (completion) completion();
    }];
}

For deleting use this:

- (void)removeContact:(Contact *)contact withBlock:(void (^)(void))completion {
    [contact deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        contact.objectId = nil;
        if (completion) completion();
    }];
}

I set the objectId to nil because I use this property in table view to see if object is allready on parse of it is just on the phone.

The problem is that if user do steps like: save, delete, save.

  1. Save: the object is created on parse with all the data.
  2. Delete: the object is deleted from parse.
  3. Save: the object is created on parse but without data (just objectId).

Is this the normal procedure? On the phone the object has allways all the data even after deletetion method. So I assume if I run save method on the object with all the data, that it will save it to the parse, even if the same object goes through deletion in the past.

Here's a picture of one empty object and one that is saved correctly with all the data: enter image description here

What are your experience with this? Enjoy resolving this issue and help making wold a better place :)

Klemen
  • 2,144
  • 2
  • 23
  • 31
  • can you post the code of where and how you are setting the fields for the contact? – Kex May 11 '15 at 14:52
  • Can you just set a flag when it's deleted rather than actually deleting the object? – Wain May 11 '15 at 15:09
  • @Wain yeah if I wont solve it this way I'll switch to flag it as deleted. – Klemen May 11 '15 at 15:29
  • @Kex i dont thing the code is necessary, I have buch of logs integrated before and after delete or save and in console every log shows that contact is filled with all the data. – Klemen May 11 '15 at 15:32

1 Answers1

2

Setting the object id to nil as you are is relying on private and undocumented features of the PFObject class. Even if it did work now it isn't guaranteed to always work.

You should either not delete the object and simply set a flag to show that it has been removed / deleted and use that for your logic.

Or, you should actually discard the local object once it's deleted and create a new object with a copy of the old objects values.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Great, I'm also thinkig about this two approaches. But I want to make sure that it there is a more convenient way. Mayber its problem with setting objectId to nil. I'll first remove this and modify the code and then see where I'll get with this. – Klemen May 11 '15 at 15:35
  • 1
    I think the last suggestion -- build a new, equivalent local object and replace the deleted one in your datasource -- is best. Doing so exactly reverses the client's state to how it was before the original save. No extra flags, and in fact PFObject provides an isNew BOOL which will be correct and reliable under this design. – danh May 11 '15 at 15:51
  • Whoops. isNew() appears to be in JS, not Objective-C. Looks like isDirty is an almost equivalent converse – danh May 11 '15 at 15:55