Ok so I have been trying something but can't get it to work yet. Basically I have this simple app that uses Parse as back-end. I fill up a table view with some images and names, then that table view takes you to a detail view that contains more images and some text.
I implemented a simple way of adding one of this objects to a "Bookmarks / Favorites" tableview, since I want this app to be simple and personally I hate apps that require logging in, I didn't want to use Parse relationships or users. So what the app does is simply get the "objectId" of the specific object being added to favorites, then it saves it to an array in NSUserDefaults, it runs a query and retrieves the objects with the object ids that match the ones in the NSUserDefaults Array.
I encountered a problem when trying to delete them from the tableview. Since I delete the object at index in the tableview, the problem is that the user adds an object Id in a specific order and the objects come with a different one since the are queried by ascending order. So lets say user adds object 1,2 & 3 to the defaults array (in that order) but the query brings back 3,1 & 2. If I delete the object at index 3 which I would expect to be object 3, it deletes object 2.
To solve this the only way I could think of is to order the query in the same order as the user adds an id to their favorites. I looked it up and I found that I can use an NSComparator to check if one is the same as the other, the information I found is kinda confusing and Im not sure how to use the comparator so if someone could point me in the right direction that would be awesome.
Thanks in advance and thanks for reading!
TLTR? Question: Can anyone provide an example on how to use an NSComparator?
I think I understand the question. You have an array like this in user defaults:
stringArray = [ @"objectId-A", @"objectId-B", @"objectId-C", @"objectId-D" ]
And you fetch parse objects somehow, and they are returned in arbitrary order, like this:
objectArray = [ <object-B>, <object-A>, <object-D>, <object-C> ]
Which becomes the datasource for a table. And then the user requests to delete an object at an indexPath
.
You're having no trouble deleting the object, since you can delete the object at index indexPath.row
from objectArray
. But how to delete the correct object from the array of ids? You can use the object's id and look it up...
deleteMe = objectArray[indexPath.row] // look up the object
deleteMeId = deleteMe.objectId! // get its id
// remove the object from objectArray
objectArray.removeAtIndex(objectArray.indexOf(deleteMe)!)
// remove the id from the id array
stringArray.removeAtIndex(stringArray.indexOf(deleteMeId)!)