From b2326ede840a27b01a51c528bd66f37ee7c27881 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Buczek?= Date: Fri, 14 Dec 2018 12:28:36 +0100 Subject: [PATCH] Fix #450, create resolved bookmarks from the oldest to newest by syncTimestamp. --- Data/sync/Sync.swift | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Data/sync/Sync.swift b/Data/sync/Sync.swift index 7ffe1c788c0..f8843ba09b3 100644 --- a/Data/sync/Sync.swift +++ b/Data/sync/Sync.swift @@ -459,12 +459,20 @@ extension Sync { guard var fetchedRecords = recordType.fetchedModelType?.syncRecords(recordJSON) else { return } // Currently only prefs are device related - if recordType == .prefs, let data = fetchedRecords as? [SyncDevice] { + if recordType == .prefs { // Devices have really bad data filtering, so need to manually process more of it // Sort to not rely on API - Reverse sort, so unique pulls the `latest` not just the `first` - fetchedRecords = data.sorted { device1, device2 in + fetchedRecords = fetchedRecords.sorted { device1, device2 in device1.syncTimestamp ?? -1 > device2.syncTimestamp ?? -1 }.unique { $0.objectId ?? [] == $1.objectId ?? [] } + } else if recordType == .bookmark { + // Bookmars are sorted to have the oldest bookmark to come in first, so it can be added before it's children. + fetchedRecords = fetchedRecords.sorted(by: { + let firstTimestamp = $0.syncTimestamp ?? 0 + let secondTimestamp = $1.syncTimestamp ?? 0 + + return firstTimestamp < secondTimestamp + }) } let context = DataController.newBackgroundContext()