Realm Threads and Array of Realm Objects


#1

I’m having an issue with my app crashing due to: SIGABRT: Realm accessed from incorrect thread

From the documentation, I know that the issue is from when I’m trying to sync several objects to realm from not the same thread.

 func saveProducts(products: [Product]) {
	DispatchQueue(label: "background").async {
	            autoreleasepool {
	        let realm = try! Realm(configuration: Config.realmConfig)
	        
	        let chunkedProducts = products.chunked(by: 2000)
	        for (idx, chunkedProduct) in chunkedProducts.enumerated() {
	            try! realm.write {
	                realm.add(chunkedProduct, update: true)
	            }
	        }
	    }
	}
}

I know I am suppose to use ThreadSafeReference, but I am uncertain how to do that with an array of objects. The array of objects are from an endpoint which serializes the json into realm objects and so I can’t do a .filter to retrieve those objects. Thanks!