0

I map my objects with ObjectMapper, that are delivered by Alamofire and persist them in Realm.

Everthing is working fine. But how can I delete objects, that exist in Realm but have been deleted in my webservice?

Update: Based on the answer below I currently ended with this code:

 if let overviewItemsArray = response.result.value{

                    do{
                        try self.realm.write{
                            self.realm.delete(self.realm.objects(OverviewItem))
                            self.realm.add(overviewItemsArray, update: true)
                        }
                    }
                    catch let err as NSError {
                        logger.error("Error with realm: \(err.localizedDescription)")
                    }
                    overviewItemsAsList.removeAll()
                    overviewItemsAsList.appendContentsOf(self.realm.objects(OverviewItem)
                        .sorted("sortOrder", ascending: true))
                    successHandler(overviewItemsAsList)
                }

Perhaps somebody has further input how to improve this. I have 10 objects of this type. But on other objects I get 1500 items.

4

3 回答 3

1

I finally figured it out and it works very well. I compute the difference of the cached data and the new data and delete it:

private func deleteOrphans(existingData: List<VotingHeader>, fetchedData:[VotingHeader]){

        guard existingData.count>0 else {
            return
        }

        let existingIDs = Set(existingData.map({$0.votingID}))
        let incomingIDs = fetchedData.map({$0.votingID})
        let idsToDelete = existingIDs.subtract(incomingIDs)

        if idsToDelete.count>0{
            let itemsToDelete =  self.realm.objects(VotingHeader).filter("votingID IN %@",idsToDelete)

            try! self.realm.write{
                self.realm.delete(itemsToDelete)
            }
        }
    }
于 2016-06-12T08:37:17.037 回答
0

You could:

-use updated_at field and update that field via id, after that delete all objects that haven't been updated or

-delete all objects and then insert new ones (expensive, I wouldn't do such a thing)

I think that you should go with the first option especially if you have a lot of data, when you get your response from web service, go through each and update corresponding records in database with NSDate() and afterwards, delete objects that are "old". Note that this is not updated_at from server but your local storage.

The other one is fine if you have small amount of data and you are sure that it will be fast and cheap.

于 2016-05-23T18:45:57.130 回答
0

This sounds like you need to implement some sort of mechanism for determining which objects were deleted on the server, and explicitly relaying that information to your local devices to reflect that change. I can think of a few possible solutions:

  • If you have control of the API, you could implement a system where your device tells the server the timestamp of when it last called the API, and the server could then return a list of the ID numbers in its response for the entries that had been deleted after that time.

  • If you're simply downloading the same list every time, rather than blow away everything in the Realm file each time, perform a manual loop through each entry in the Realm file and see if it has a corresponding entry in the downloaded list. If not, delete it. Granted, this will still be a relatively slow operation, but it would be much faster than deleting/recreating everything each time.

  • Since it sounds like Realm is being used to cache server information here, you could just set a really strict expiry time on the Realm objects that would delete them if it is exceeded, or extended if the object is refreshed from the server.

I hope that helps!

于 2016-05-24T08:38:02.720 回答