2

I am using PFFile to store images. If the image is already downloaded, I want to access it directly. If not, I want to use the background methods. However, if I use getData, I get the following warning:

Warning: A long-running operation is being executed on the main thread. 
 Break on warnBlockingOperationOnMainThread() to debug.

Since I know that the data is available, this warning is unnecessary and clutters my log. Is there any way to access PFFile's data without triggering a warning?

Philipp
  • 1,903
  • 2
  • 24
  • 33

1 Answers1

3

You are getting the warning because the data is not available locally and getData is a synchronous call to fetch the data from the server. When getData is called, it blocks the main thread - the UI - and stops all app interaction until the data downloads, which is why you are getting the error. Generally, blocking the UI to do a background operation, such as downloading, is very much frowned upon.

I would use the isDataAvailable property of PFFile to check if the data is available locally. If it isn't, use getDataInBackgroundWithBlock: to fetch the data in the background. You can use the completion method supplied by that call to update your imageView.

UPDATE - You can also wrap the getData call in a dispatch_async block, which will move the operation to another thread, therefore removing it from the main thread and getting rid of the warning.

John Davis
  • 223
  • 1
  • 6
  • Hi, thanks for your answer. However, I am already checking with isDataAvailable, and I can see that the call of getData actually doesn't use the network. So it is as I already explained above: I get this warning even though it's unnecessary. – Philipp Dec 30 '14 at 06:31
  • I would still use `getDataInBackground`. That is the standard way to do what you are doing. Loading the images asynchronously will be better for performance, even if the data is cached locally. Either that, or wrap each `getData` call in a dispatch_async block. Either way, that warning won't go away until you take your call off of the main thread. – John Davis Dec 30 '14 at 11:13
  • I would also love to use getDataInBackground. However, in that particular case, I can't: I use PFFiles to download images and have to display them at some point. Since I use a framework for display, I cannot use Parse's PFImage. If I use getDataInBackground, there is a noticable flickering before the image is actually displayed. I could build a small cache around PFFile, but that seems silly, since Parse already does the caching for me - hence my question. – Philipp Dec 30 '14 at 15:04
  • 1
    I tried dispatch_async and it works. Would you mind editing your answer and mentioning dispatch_async there, so that I can accept it as the correct answer? Thanks! – Philipp Dec 30 '14 at 15:08