Swift Error: Realm accessed from incorrect thread - swift

I am attempting to use the Realm library to persist data within my application. However, I keep running into the same error code: "Realm accessed from incorrect thread". I attempted to resolve this issue by creating a Realm-specific Dispatch Queue, and wrapping all of my Realm calls in it.
Here is what my "RealmManager" class looks like right now:
import Foundation
import RealmSwift
class RealmManager {
fileprivate static let Instance : RealmManager = RealmManager()
fileprivate var _realmDB : Realm!
fileprivate var _realmQueue : DispatchQueue!
class func RealmQueue() -> DispatchQueue {
return Instance._realmQueue
}
class func Setup() {
Instance._realmQueue = DispatchQueue(label: "realm")
Instance._realmQueue.async {
do {
Instance._realmDB = try Realm()
} catch {
print("Error connecting to Realm DB")
}
}
}
class func saveObjectArray(_ objects: [Object]) {
Instance._realmQueue.async {
do {
try Instance._realmDB.write {
for obj in objects {
Instance._realmDB.add(obj, update: .all)
}
}
} catch {
print("Error Saving Objects")
}
}
}
class func fetch(_ type: Int) -> [Object] {
if let realm = Instance._realmDB {
let results = realm.objects(Squeak.self).filter("type = \(type)")
var returnArray : [Object] = []
for r in results {
returnArray.append(r)
}
return returnArray
}
return []
}
I am calling Setup() inside of didFinishLaunchingWithOptions to instantiate the Realm queue and Realm Db instance.
I am getting the error code inside of saveObjectArray at:
try Instance._realmDB.write { }
This seems to simply be a matter of my misunderstanding of the threading requirements of Realm. I would appreciate any insight into the matter, or a direction to go in from here.

This issue is that you fetch your Realm data on a different thread than you save it.
To fix the error, the code within fetch will also need to run on the Realm thread that you have created.
I think this article does a good job of explaining multi-threading in Realm and particularly recommend paying attention to the three rules outlined in the article.

Related

How would I access a published variable from another class?

So I am currently working on my app and have come across a problem where I have to access a published variable in another observable class.
Here is some code on what I am trying to do
class PeriodViewModel: ObservableObject {
#Published var value = 1
}
class DataViewModel: ObservableObject {
#ObservedObject var periodViewModel = PeriodViewModel()
periodViewModel.value = 1
}
How would I be able to access the updated variable from periodViewModel in dataViewModel? Thanks.
[Note]: Ignore all the variables I am just showing the the flow of using different mangers.
Here is an example of function that I am using for my FirebaseDatabaseManage. You can see a clouser is passing into the function parameter. When your firebase insert function response after async you need to call your clouser which I named as completion.
class FirebaseDatabaseManager {
public func insertRecipe(with recipe: RecipeModel, completion: #escaping (Bool, String) -> Void) {
SwiftSpinner.show("Loading...")
let userID = UserDefaultManager.shared.userId
database.child(FireBaseTable.recipes.rawValue).child(userID).childByAutoId().setValue(recipe.convertToDictionary!, withCompletionBlock: { error, ref in
SwiftSpinner.hide()
guard error == nil else {
completion(false, "failed to write to database")
return
}
completion(true, ref.key ?? "no key found")
})
}
}
Now look at my ViewModel class in which I am calling my FirebaseManager method. On calling of completion I am updating my #Publisher Which you can use to update your UI.
class RecipeViewModel: ObservableObject {
#Publisher var id = 0
func createRecipe() {
FirebaseDatabaseManager.shared.insertRecipe(with: self.recipeModel) { status, id in
self.id = id
}
}
}
Hope this help your in understand your concepts.

How should I interpret this GCD threading issue message?

The XCODE TSAN thread analyser is throwing up a threading issue:
Data race in generic specialization <Foundation.UUID> of Swift._NativeSet.insertNew(_: __owned τ_0_0, at: Swift._HashTable.Bucket, isUnique: Swift.Bool) -> () at 0x10a16b300
This only occurs in a release build, and its Data race in generic specialization that has my attention.
It pin-points the addID function. But I cannot see the issue. Here is the relevant code snippet:
final class IDBox {
let syncQueue = DispatchQueue(label: "IDBox\(UUID().uuidString)", attributes: .concurrent)
private var _box: Set<UUID>
init() {
self._box = []
}
var box: Set<UUID> {
get { syncQueue.sync { self._box } }
set { syncQueue.async(flags: .barrier) {
self._box = newValue
}
}
func addID(_ id: UUID) {
syncQueue.async(flags: .barrier) {
self._box.insert(id)
}
}
}
I found the error. The read-writer implementation was correct, but I found a call idBox.box.insert(id) rather than idBox.addId(id) elsewhere in the code base.
As an aside I can see why using an actor would prevent this kind of bug...although actors come with other constraints, for example not being able to conform to Codable. But that's for another post.

How can I type check a generic then cast to it in swift?

I like the ideas presented in this post, about making database-agnostic, protocol-oriented code.
So say I have a protocol such as:
protocol Database {
func loadObjects<T>(matching query: Query) -> [T]
func loadObject<T>(withID id: String) -> T?
func save<T>(_ object: T)
}
where Query is a struct that has filter and sort specifiers.
Then, given a persistence framework, such as Realm or CoreData, I can just support this protocol, as such:
extension NSManagedObjectContext: Database {
...
}
extension Realm: Database {
...
}
extension MockedDatabase: Database {
...
}
extension UITestingDatabase: Database {
...
}
The issue arises when I would want to use CoreData.
If we look at the method:
func loadObjects<T>(matching query: Query) -> [T]
I have no way to 'cast' T to NSManagedObject.
For example, my desired implementation might be something like:
extension NSManagedObjectContext: Database {
func loadObjects<T>(matching query: Query<T>) -> [T] {
// you need a fetch request for these models. This guard statement compiles. How do we make it work with NSFetchRequestResult however?
guard T.self is NSManagedObject.Type else {
return []
}
// This line below Fails compiling. Type 'T' does not conform to protocol 'NSFetchRequestResult'
var request = NSFetchRequest<T>(entityName: String(describing: T))
// then set sortDescriptors and predicate, etc.
var objects: [T] = []
self.performAndWait {
do {
if let results = try self.fetch(request!) as? [T] {
objects = results
}
} catch let error {
print("Error fetching: \(error.localizedDescription)")
}
}
return objects
}
}
So if I can determine if T's Type is a kind of NSManagedObject, then is it possible to instantiate a NSFetchRequest by 'casting' T to something that will work? It would seem I can't cast or force T to be anything.
Database is a technology-agnostic protocol and therefore shouldn't know anything about Core Data. I'd like to do this in the event I need to change my data persistence framework.
How might I accomplish this in Swift? Would I have to add to the Model protocol to return optionals that would be used for the given frameworks I would support? Or make them support NSFetchRequestResult? I would rather that only the implementation of the protocol need to care about the details of the persistence framework.
Rather than checking the type at runtime constrain the type at compile time, for example
extension NSManagedObjectContext: Database {
func loadObjects<T: NSManagedObject>(matching query: Query<T>) -> [T] {
let request = NSFetchRequest<T>(entityName: String(describing: T.self))
var objects = [T]()
self.performAndWait {
do {
objects = try self.fetch(request) // a generic fetch request returns an array of the generic type or throws an error
} catch let error {
print("Error fetching: \(error.localizedDescription)")
}
}
return objects
}
}
It looks like I can answer my own question. One can further constrain generic types by overloading them, so that the calling code can remain the same, but what gets called is dependent on the type you pass into it.
This code below demonstrates this in a playground:
public protocol Action {
func doSomething<T>(to object: T)
}
public class MyActor {
}
extension MyActor: Action {
// works for any type
public func doSomething<T>(to object: T) {
print("was generic")
}
// but if you constrain the type and your object fits that constraint...
// this code is called (same method signature)
public func doSomething<T: NSObject>(to object: T) {
print("was an object")
}
}
class MyObject: NSObject {
var name: String = "Object"
}
struct MyStruct {
var name: String = "Struct"
}
let actor = MyActor()
let object = MyObject()
let value = MyStruct()
actor.doSomething(to: value) // prints 'was generic'
actor.doSomething(to: object) // prints 'was an object'
So in the original example, I would then support Database for CoreData with:
extension NSManagedObjectContext: Database {
func loadObjects<T>(matching query: Query<T>) -> [T] {
return [] // return an empty array because we only support NSManagedObject types
}
func loadObjects<T: NSManagedObject>(matching query: Query<T>) -> [T] {
let request = NSFetchRequest<T>(entityName: String(describing: T.self))
var objects = [T]()
self.performAndWait {
do {
objects = try self.fetch(request) // a generic fetch request returns an array of the generic type or throws an error
} catch let error {
print("Error fetching: \(error.localizedDescription)")
}
}
return objects
}
}

Create Generic Realm repository in Swift

I have been struggling with solving a problem which I am pretty sure leads me to adopt the Type-Erasure technique but I am not 100% sure. I have tried several different times and have felt close but ultimately have failed. I will try to simplify my problem. Lets say you have an entity
struct Expense {
var id: Int?
var amount: Double = 0
}
and an equivalent Realm Object
class RealmExpense: Object {
let id = RealmOptional<Int>()
let amount = RealmOptional<Double>()
var entity: Expense {
return Expense(id: id.value, amount: amount.value)
}
}
Note that I can convert from RealmExpense to Expense by using the entity variable.
Then, I have another protocol
protocol ExpenseRepository: class {
func getAll() -> [Expense]
//...other CRUD methods
}
and finally, a class
class ExpenseRealmRepository: ExpenseRepository {
private let realm = try! Realm()
func getAll() -> [Expense] {
return realm.objects(RealmExpense.self).flatMap { $0.entity }
}
func insert(item: Expense) {
try! realm.write {
realm.add(RealmExpense(expense: item))
}
}
//...implementation of other CRUD methods
}
Now, this works fine, but I have many entities, and this is repetitive code that I would like to refactor but every attempt I have made to make this generic throws one compiler error or another. What I would like is to basically be able to create a class
class RealmRepository<RepositoryType: Object, EntityType> {
private let realm = try! Realm()
func getAll() -> [EntityType] {
return realm.objects(RepositoryType.self).flatMap { $0.entity }
}
func insert(item: EntityType) {
try! realm.write {
realm.add(RepositoryType(item))
}
}
//...the rest of the CRUD methods.
}
My main problem is that it seems that Swift generics do not allow this type of behavior. Maybe there is a better overall approach that I have not explored.
Note: Small addendum to Dave Weston's answer below:
The answer given by David Weston below is correct (and excellent) but there is currently a Swift bug that prevents the Realm Entity initializer from being used. See:
https://github.com/realm/realm-cocoa/pull/2514
Basically, the solution is to use the default initializer for Realm Objects which accept a dictionary. Since I am already using the ObjectMapper library to convert entities to and from JSON this was as simple as changing
func insert(item: T.EntityType) {
realm.write {
realm.add(RealmEntityType(item)) //compiler error when initializing RealmEntityType
}
}
to
func insert(item: T.EntityType) {
let realmItem = RealmEntityType()
realmItem.setValuesForKeys(item.toJSON()) //needs to be a dictionary
try! realm.write {
realm.add(realmItem)
}
}
This was an interesting problem. I'm not sure how you'll be using these repository objects, but based on your question so far, I didn't need to use type erasure.
First, we define the generic hierarchy that we need:
// implemented by your immutable structs
protocol Entity {
}
// Implemented by RealmSwift.Object subclasses
protocol RealmEntity {
associatedtype EntityType
init(_ entity: EntityType)
var entity: EntityType { get }
}
// protocol to define the operations one can perform on a repository
protocol Repository {
associatedtype EntityType
func getAll() -> [EntityType]
func insert(item: EntityType)
}
// A RealmRepository that implements the Repository protocol on top of Realm
class RealmRepository<T>: Repository where T: RealmEntity, T: Object, T.EntityType: Entity {
typealias RealmEntityType = T
private let realm = Realm()
internal func insert(item: T.EntityType) {
realm.write {
realm.add(RealmEntityType(item))
}
}
internal func getAll() -> [T.EntityType] {
return realm.objects(T.self).flatMap { $0.entity }
}
}
Now that we have the infrastructure, we can create an ExpenseRepository to show that all of this stuff compiles:
struct Expense: Entity {
var id: Int
var amount: Double
}
class RealmExpense: Object, RealmEntity {
typealias EntityType = Expense
let id: RealmOptional<Int>
let amount: RealmOptional<Double>
required init(_ entity: EntityType) {
id = RealmOptional(entity.id)
amount = RealmOptional(entity.amount)
}
var entity: Expense {
return Expense(id: id.value!, amount: amount.value!)
}
}
var expenseRepository = RealmRepository<RealmExpense>()
This may not compile in your project, since I created in a playground that didn't import Realm, but it should give you an idea.
Now, if you want to store an array of repositories of different types, or a variable of type Repository, that's when you'll need type erasure, and have to create an AnyRepository.
for all those who are looking into such kind of solution, I have made a sample taking help from the discussions above. I am posting the link for the repo below
https://github.com/Rj707/GenericRealm
I am trying to write a library, you can find it on:
https://github.com/rusito-23/RealmDAO
UPDATE:
The basic idea is to handle all your Realm transactions inside a class called GenericDAO, that has two associated types, one that extends from Object, and another that extends from Transferrable (this way we can handle a copy of the object and not the object itself).
There is also an extra protocol and his method to handle Autoincremental primary keys!
It should be as simple to use as:
let movieDAO = GenericDAO<Movie>()
movieDAO.findAll(completion: { /*-- YOUR CODE --*/})
You can find more information on the link I sent!
Hope it helps!

Realm data Insertion took over 7mins for big size json

I use Alamofire to download big json data which was about around 7MB and use RealmSwift to store data at realmobject and SwiftyJSON to parse.My realm object insertion after it finished download the json seems really slow at insertion.Was something wrong with my bad code?Please guide me.
First of all I will show simplest Json :
{
{
"Start" : "40000",
"End" : "1000000",
"Name" : "Smith",
"Address" : "New York"
},{...},more than 7000 records...
}
Here is my AlamofireAPI Protocol
import Foundation
import Alamofire
import SwiftyJSON
protocol RequestDataAPIProtocol{
func didSuccessDownloadingData(results:JSON,statusCode : Int)
func didFailDownloadingData(err : NSError)
}
class RequestDataAPI{
var delegate : RequestDataAPIProtocol
init(delegate: RequestDataAPIProtocol){
self.delegate=delegate
}
func post(requestURL:String,param:[String:String]){
Alamofire.request(.GET, requestURL, parameters: param)
.validate(statusCode: [200,500])
.responseJSON(completionHandler: { (response: Response<AnyObject, NSError>) -> Void in
if let error = response.result.error {
self.delegate.didFailDownloadingData(error)
} else if let jsonObject: AnyObject = response.result.value {
let json = JSON(jsonObject)
self.delegate.didSuccessDownloadingData(json,statusCode: (response.response?.statusCode)!)
}
})
}
func doRequestSiteData(token : String){
post(REQUEST_DATA,param:["data":"All","token":token])
}
}
Here is my Realm DB Helper
import Foundation
import RealmSwift
class DBHelper{
func insertUserData(list: UserList){
do {
let realm = try! Realm()
try realm.write({ () -> Void in
realm.add(list,update: true)
})
} catch let error as NSError {
print("Insert Error : \(error)")
}
}
}
Here is my realm modelObject
import Foundation
import RealmSwift
class UserList: Object {
dynamic var start : String = ""
dynamic var end : String = ""
dynamic var name : String = ""
dynamic var address : String = ""
}
And Final Code,View Controller,
class ViewController : UIViewController , RequestDataAPIProtocol{
var dbHelper = DBHelper()
var requestDataAPI : RequestDataAPI!
override func viewDidLoad() {
super.viewDidLoad()
requestDataAPI = RequestDataAPI(delegate : self)
}
override func viewDidAppear(animated : Bool){
//assume there is one token to request data
requestDataAPI.doRequestSiteData(token)
}
func didSuccessDownloadingData(results: JSON, statusCode: Int){
dispatch_async(dispatch_get_main_queue(), {
print("Downloaded JSON")
switch statusCode{
case 200 :
if results.count > 0{
if let users = results.array {
for user in users{
let userList=UserList()
userList.start=user["Start”].stringValue
userList.end=user[“End”].stringValue
userList.name=user[“Name”].stringValue
userList.address =user[“Address”].stringValue
self.dbHelper.insertUserData(userList)
}
}
}
// took about more than 7 mins
print(“Insertion Done”)
break
case 500,401,400 :
//TODO:
default : break
}
})
}
}
I know its really stupid about describing all the code steps,I write as simple as i could for my working flow for inserting json data into realm swift.
I just want all to know about my working flow is good or bad when handling so many json data,and also insertion.
The reason why I am asking this was the data insertion took about more than 7 mins to finish.
So,I really need help,to make optimize at my code.
Any guide?
UPDATE : I use Delegate and Protocol from RequestDataAPI which i learn that style from JamesQueue Tutorial because I am completely beginner who is still learning Swift.ViewController is updated.That is my whole process detail,no more code left.Editing my question or answer a new is appreciated for code optimizing.
insertUserData method method opens transactions so many times in the loop. To commit transaction is a little bit expensive operation.
Can you try to put out to open/commit a transaction outside of the loop?
In other words, open the transaction before entering the loop, and commits the transaction once after the end of the loop. Like the following:
if results.count > 0 {
if let users = results.array {
let realm = try! Realm()
try realm.write {
for user in users{
let userList=UserList()
userList.start=user["Start”].stringValue
userList.end=user[“End”].stringValue
userList.name=user[“Name”].stringValue
userList.address =user[“Address”].stringValue
realm.add(userList,update: true)
}
}
}
}
I have fixed slow insertion issue by using this code.
func addAsBatch<T: Object>(_ data: [T]) {
if !isRealmAccessible() { return }
let realm = try! Realm()
realm.refresh()
realm.beginWrite()
for object in data {
realm.add(object)
}
try? realm.commitWrite()
}
Showing function use with your example -
let userList = UserList()
userList.start = user["Start”].stringValue
userList.end = user[“End”].stringValue
userList.name = user[“Name”].stringValue
userList.address = user[“Address”].stringValue
addAsBatch(userList)