From e30c007eb0171ecc8a6c9b95850a1a7247763a2f Mon Sep 17 00:00:00 2001
From: SajuThomasPattarmatam
 <67682109+SajuThomasPattarmatam@users.noreply.github.com>
Date: Wed, 29 Jun 2022 02:40:14 +0530
Subject: [PATCH] Fix crash due to db operation on different thread (#138)

* Fix issue where field name is used for fetching property type instead of property name, resulting in crash when field name is "FieldName" and property name is "fieldname", since it wont get the property type as data

* add in perform and wait since its a db operation, else it can cause a crash
---
 Sources/ContentfulPersistence/SynchronizationManager.swift | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/Sources/ContentfulPersistence/SynchronizationManager.swift b/Sources/ContentfulPersistence/SynchronizationManager.swift
index fed1a94e..3fca3910 100644
--- a/Sources/ContentfulPersistence/SynchronizationManager.swift
+++ b/Sources/ContentfulPersistence/SynchronizationManager.swift
@@ -120,8 +120,10 @@ public class SynchronizationManager: PersistenceIntegration {
      - parameter limit: Number of elements per page. See documentation for details.
      */
     public func sync(limit: Int? = nil, dbVersion: Int = DBVersions.default.rawValue, then completion: @escaping ResultsHandler<SyncSpace>) {
-        // If the migration is required - it will be performed before any new changed takes affect
-        migrateDbIfNeeded(dbVersion: dbVersion)
+        persistentStore.performAndWait { [weak self] in
+            // If the migration is required - it will be performed before any new changed takes affect
+            self?.migrateDbIfNeeded(dbVersion: dbVersion)
+        }
         
         resolveCachedRelationships { [weak self] in
             self?.syncSafely(limit: limit, then: completion)