1

I have a case where I am saving data to Realm Database inside Dispatch.global in background thread then inside same thread I have called Dispatch.main.async to fetch data from Realm and update UITableView Data.

The problem is I am getting less number of Data (data.count). Suppose total data count is 10 then sometimes I am get all the data sometimes and sometimes less then 10.

Why does this happen?

Following is the example code snippet

func getData(data: [String]) {
    DispatchQueue.global(qos: .background).async {
        RealmManager.removeDataFromRealm()
        RealmManager.saveDataToRealm(data)
        Dispatch.main.async {
            let dataFromRealm = RealmManager.getDataFromRealm()
            self.sendDataToUI(dataFromRealm)
        }
    }
}

In above code removeDataFromRealm(), saveDataToRealm(data), getDataFromRealm() are realm class static func where I save, remove, get data from realm database

I have debug the code from all the aspect that I understand and it saves (saveDataToRealm(data)) all the data and then fetch (getDataFromRealm()) the data, according to my understanding then why it sends me less number of data sometimes

There is no filter applied to RealmManager getDataFromRealm() static methods, while fetching data.

Suppose above code goes in race condition then what happens in below code snippet

    func getImageFromServer (url: URL) {
        DispatchQueue.global(qos: .background).async {
            do {
                let data = try Data(contentsOf: url)
                DispatchQueue.main.async {
                    self.imageView.image = UIImage(data: data)
                }
            }catch {
                print(error)
            }
        }
    }

As getImageFromServer() first fetch data then Dispatch.main.async is executed after converting "Data(contentsOf: url)" to data which obviously is time taking.

Why does it work differently in above cases?

halfer
  • 19,824
  • 17
  • 99
  • 186
Manish Patel
  • 117
  • 2
  • 9
  • Your realm calls are all probably time consuming, which means your fetch is initiated before the save is completed. – Rakesha Shastri Nov 16 '18 at 15:07
  • Are the implementations of `RealmManager.removeDataFromRealm` and `RealmManager.saveDataToRealm` asynchronous or synchronous? – rmaddy Nov 16 '18 at 16:13
  • @RakeshShastri I debugged the code and as per debugging break points it remove data then save data then fetch data.. – Manish Patel Nov 17 '18 at 09:20
  • @rmaddy As per my understanding Realm library works asynchronously. My method implementation are synchronous – Manish Patel Nov 17 '18 at 09:30
  • RealmManager.saveDataToRealm(data) is async function ? i mean inside saveDataToRealm is any operation perform asynchronously ? – Mihir Mehta Nov 19 '18 at 05:54
  • Dispatch.main.async block inside async block use to perform operations in main thread. so according to that you fetch data async and then when you wanna display data in ui use main thread. If you still face the issue there must be issue with you fetch query. There must be some kind of limit in query check it in depth. – Anas Sabir Nov 19 '18 at 06:50
  • @mihirmehta Their is no async operations inside RealmManager.saveDataToRealm(data), But I have doubt with Realm write method lib – Manish Patel Nov 19 '18 at 11:00
  • @AnasSabir i don't think there is any issue with query limit because some time i am getting all the data And about async code all of my code inside realm func are sync but i have doubt with realm methods weather they are sync or async – Manish Patel Nov 19 '18 at 11:10

1 Answers1

0

If your RealmManager.removeDataFromRealm() and/or RealmManager.saveDataToRealm(data) is async then you have got yourself into a race condition here as there is nothing guarantees that your data is saved before the code in DispatchQueue.main is executed. What you can do is to use DispatchGroup to wait for the two methods above to finish before entering DispatchQueue.main.async.

To answer the question in the title, if you are in global queue and then execute codes in global queue, what swift does is essentially switching from the former to the later.

Linh Ta
  • 593
  • 6
  • 11