update and delete in realm xamarin


Problem:

i have a following json that i am Add and Update in realm database

JSON

"listActivityTypeMaster": [
    {
        "id": "NV_22_06",
        "name": "NV_22_06"
    },
    {
        "id": "POC",
        "name": "POC"
    }
]

class that stores above json

public class ActivityType : RealmObject, IEntity
{
    [PrimaryKey]
    public string id { get; set; }
    public string name { get; set; }


}

to create and update i am using following realm statement

realm.WriteAsync(tempRealm =>{


    tempRealm.Add(data, true);


});

Now what i want is if my JSON gets changed by removing one entry then my realm should also delete that entry when i fire tempRealm.Add(data, true);

"listActivityTypeMaster": [

    {
        "id": "POC",
        "name": "POC"
    }
]

i do understand that tempRealm.Add(data, true); is for updating record and it will add a new entry of primary key does not exist otherwise it will replace it. but is there any way if primary key is not exist then it will delete that record from realm


Solution:

That is not possible, and there are very few cases where that would be desirable. As a workaround, after updating, you can execute:

var activities = realm.All<ActivityType>().ToArray();
var ids = new HashSet<string>(data.Select(d => d.id));
realm.Write(() =>
{
    foreach (var activity in activities)
    {
        if (!ids.Contains(activity.Id))
        {
            realm.Remove(activity);
        }
    }
});

Recent Tips

  1. Grabbing text between Processing Instructions in XSLT
  2. Foreign key refering to multiple tables
  3. Run a visual studio 2010 Windows7 project on a Windows XP machine
  4. How to apply groupname to HTML radio buttons in asp.net?
  5. What is the most stable modal dialog implementation across browsers to use from javascript for a web app?
  6. Move, Remove, and Replace information from Google Sheets with new entries using Google Script
  7. How can I get rid of jerkiness in WinForms scrolling animation?
  8. Update MySQL table column from another table entities
  9. Shoot fireball once in unity C#
  10. Adding a table of contents to a Microsoft Word Document using vbs
  11. Change locale in android app (onto Hindi)
  12. How do I stop selenium automation if 20% or 1st 20 test cases test methods are failed?
  13. Sharepoint with silverlight app
  14. What's the best way to write robots.txt for github pages using multiple repos?
  15. Biopython: Cant use .count() for biopython
  16. How can I find out the token balance of an address?
  17. ref value is undefined in vue (modal, textarea, $refs)
  18. Azure - HDInsight Hbase Data Insertion Failed
  19. SignalR overwriting OnConnected(), OnDisconnected()
  20. DatePickerDialog displays with two borders
  21. "type 'double' is not a subtype of type 'int' in type cast" error in flutter. What should i do?
  22. hiding the autocomplete list when user click outside the textbox is not working as expected
  23. JSF IceFaces basic problem with redisplaying input value
  24. How to validate material ui TextField in reactjs?
  25. Go and MongoDB connection won't work with panic log "no reachable server"
  26. WordPress Posts Pagination Not Working
  27. F# sprintf won't print in interactive console
  28. Spring Integration get FTP files recursively with outbound-gateway
  29. Jade mixins not getting working from external file
  30. Can not access defined exports from the webpack bundle?