What happens if I put Dispatch.main.async block inside Dispatch.global.async?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








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 this happens please help me to understand this..



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.



Please help me to understand why it works differently in above cases... Thank you in advance










share|improve this question
























  • 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 9:20











  • @rmaddy As per my understanding Realm library works asynchronously. My method implementation are synchronous

    – Manish Patel
    Nov 17 '18 at 9:30











  • RealmManager.saveDataToRealm(data) is async function ? i mean inside saveDataToRealm is any operation perform asynchronously ?

    – mihir mehta
    Nov 19 '18 at 5:54

















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 this happens please help me to understand this..



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.



Please help me to understand why it works differently in above cases... Thank you in advance










share|improve this question
























  • 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 9:20











  • @rmaddy As per my understanding Realm library works asynchronously. My method implementation are synchronous

    – Manish Patel
    Nov 17 '18 at 9:30











  • RealmManager.saveDataToRealm(data) is async function ? i mean inside saveDataToRealm is any operation perform asynchronously ?

    – mihir mehta
    Nov 19 '18 at 5:54













1












1








1


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 this happens please help me to understand this..



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.



Please help me to understand why it works differently in above cases... Thank you in advance










share|improve this question
















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 this happens please help me to understand this..



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.



Please help me to understand why it works differently in above cases... Thank you in advance







swift realm grand-central-dispatch dispatch-async dispatch-queue






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 5:48







Manish Patel

















asked Nov 16 '18 at 14:40









Manish PatelManish Patel

114




114












  • 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 9:20











  • @rmaddy As per my understanding Realm library works asynchronously. My method implementation are synchronous

    – Manish Patel
    Nov 17 '18 at 9:30











  • RealmManager.saveDataToRealm(data) is async function ? i mean inside saveDataToRealm is any operation perform asynchronously ?

    – mihir mehta
    Nov 19 '18 at 5:54

















  • 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 9:20











  • @rmaddy As per my understanding Realm library works asynchronously. My method implementation are synchronous

    – Manish Patel
    Nov 17 '18 at 9:30











  • RealmManager.saveDataToRealm(data) is async function ? i mean inside saveDataToRealm is any operation perform asynchronously ?

    – mihir mehta
    Nov 19 '18 at 5:54
















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





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





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 9:20





@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 9:20













@rmaddy As per my understanding Realm library works asynchronously. My method implementation are synchronous

– Manish Patel
Nov 17 '18 at 9:30





@rmaddy As per my understanding Realm library works asynchronously. My method implementation are synchronous

– Manish Patel
Nov 17 '18 at 9:30













RealmManager.saveDataToRealm(data) is async function ? i mean inside saveDataToRealm is any operation perform asynchronously ?

– mihir mehta
Nov 19 '18 at 5:54





RealmManager.saveDataToRealm(data) is async function ? i mean inside saveDataToRealm is any operation perform asynchronously ?

– mihir mehta
Nov 19 '18 at 5:54












1 Answer
1






active

oldest

votes


















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.






share|improve this answer























  • Please check my edited question, I appreciate your answer Thank you

    – Manish Patel
    Nov 19 '18 at 5:49












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339971%2fwhat-happens-if-i-put-dispatch-main-async-block-inside-dispatch-global-async%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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.






share|improve this answer























  • Please check my edited question, I appreciate your answer Thank you

    – Manish Patel
    Nov 19 '18 at 5:49
















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.






share|improve this answer























  • Please check my edited question, I appreciate your answer Thank you

    – Manish Patel
    Nov 19 '18 at 5:49














0












0








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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 2:55









Linh TaLinh Ta

15817




15817












  • Please check my edited question, I appreciate your answer Thank you

    – Manish Patel
    Nov 19 '18 at 5:49


















  • Please check my edited question, I appreciate your answer Thank you

    – Manish Patel
    Nov 19 '18 at 5:49

















Please check my edited question, I appreciate your answer Thank you

– Manish Patel
Nov 19 '18 at 5:49






Please check my edited question, I appreciate your answer Thank you

– Manish Patel
Nov 19 '18 at 5:49




















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339971%2fwhat-happens-if-i-put-dispatch-main-async-block-inside-dispatch-global-async%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線