private void StoreChangedObjects(List <DownloadResponseItem> changedObjects, Realm realmiusData, Realm realmLocal) { var problematicChangedObjects = new List <DownloadResponseItem>(); realmLocal.Write(() => { foreach (var changeObject in changedObjects) { _downloadIndex++; string changedObjectInfo = !changeObject.IsDeleted ? changeObject.SerializedObject : "{" + $"\n \"{nameof(changeObject.Type)}\": \"{changeObject.Type}\",\n \"{nameof(changeObject.MobilePrimaryKey)}\": \"{changeObject.MobilePrimaryKey}\"\n \"{nameof(changeObject.IsDeleted)}\": \"{changeObject.IsDeleted}\"\n" + "}"; Logger.Debug( $"Down:{_downloadIndex}, {changeObject.Type}.{changeObject.MobilePrimaryKey}: {changedObjectInfo}"); try { var syncStateObject = FindSyncStatus(realmiusData, changeObject.Type, changeObject.MobilePrimaryKey); if (syncStateObject == null) { syncStateObject = new ObjectSyncStatusRealm { Type = changeObject.Type, MobilePrimaryKey = changeObject.MobilePrimaryKey, SyncState = (int)SyncState.Synced, }; realmiusData.Write(() => { realmiusData.Add(syncStateObject); }); //Logger.Log.Debug($" Created SyncStatus {changeObject.MobilePrimaryKey}"); } var objInDb = (IRealmiusObjectClient)realmLocal.Find(changeObject.Type, changeObject.MobilePrimaryKey); if (objInDb == null) { //object not found in database - let's create new one if (!changeObject.IsDeleted) { IRealmiusObjectClient obj = (IRealmiusObjectClient)Activator.CreateInstance(_typesToSync[changeObject.Type].Type); try { _skipObjectChanges = true; //realmLocal.Write( // () => // { AssignKey(obj, changeObject.MobilePrimaryKey, realmLocal); var success = Populate(changeObject.SerializedObject, obj, realmLocal); realmLocal.AddSkipUpload(obj, false); if (!success) { problematicChangedObjects.Add(changeObject); } //}); } finally { _skipObjectChanges = false; } } } else { //object exists in database, let's update it var uploadItems = realmiusData.All <UploadRequestItemRealm>() .Where( x => x.Type == changeObject.Type && x.PrimaryKey == changeObject.MobilePrimaryKey); if (changeObject.IsDeleted) { //realmLocal.Write( // () => // { realmiusData.Write( () => { syncStateObject.SerializedObject = SerializeObject(objInDb); syncStateObject.IsDeleted = changeObject.IsDeleted; }); realmLocal.Remove((RealmObject)objInDb); //}); } else { try { _skipObjectChanges = true; //realmLocal.Write( // () => // { var success = Populate(changeObject.SerializedObject, objInDb, realmLocal); if (!success) { problematicChangedObjects.Add(changeObject); } //we need to apply all "uploads" to our object to keep it consistent foreach (UploadRequestItemRealm uploadRequestItemRealm in uploadItems) { Populate(uploadRequestItemRealm.SerializedObject, objInDb, realmLocal); } realmiusData.Write( () => { syncStateObject.SerializedObject = SerializeObject(objInDb); syncStateObject.IsDeleted = changeObject.IsDeleted; }); //}); } finally { _skipObjectChanges = false; } } //Logger.Log.Info(" syncState.Change.Finished"); } } catch (Exception ex) { Logger.Debug($"error applying changed objects {ex}"); } } }); if (problematicChangedObjects.Count < changedObjects.Count) { StoreChangedObjects(problematicChangedObjects, realmiusData, realmLocal); } }