Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSFileManager: Continue writing to disk in background?

in my iPhone app, I am using the default NSFileManager to copy and move some data around. The amount of data may be some MB, so it may take several seconds to finish copying. If the user now starts a copy process and quits the application, can I continue writing to disk in background mode? If so, how? I do not want to start a new copy process, but only finish the running one.

I am currently using two kinds of method calls:

[imageData writeToFile:path atomically:YES];

and

[fm copyItemAtPath:sourcePath toPath:destinationPath error:&error];

Both calls are wrapped in another method and are performed in the background via -performSelectorInBackground:withObject:.

Another question is if I can get any information on how far the writing operation has progressed or at least if it has already finished?

EDIT: Some more information: Currently, I did not implement any background tasks at all. What happens to running disk operations when the user presses the home button? Are they just cut off and the file is incomplete? Can I continue writing on the next start? Are they canceled and the incomplete file is removed?

To sum it all up: I am writing data to disk and I want it to stay consistent when the user presses the home button during a writing operation. How do I achieve this?

like image 418
Björn Marschollek Avatar asked Feb 26 '23 11:02

Björn Marschollek


1 Answers

By default you app does not really finishes when the user press the home button. Hence it should finish the that task as long is does not take too long. If it takes long then please take a look at this question: How to implement Task completion

One thing: I think you are confused about what performSelectorInBackground:withObject: really does. The background used in "... I continue writing to disk in background mode" and the background in "performSelectorIn Background :withObject: " are not the same background

The former background: Is when you app becomes invisible to the user, but is still running, at least for a while. (When the user presses twice the home button and change to another app)

The latter background: Refers to a background thread, which is the opposite to the main thread.

In this case, if you use or not performSelectorInBackground:withObject: it will have no effect in whether you app can do it background mode or not. These are completely different things

You can set BOOL finished = YES right after [fm copyItemAtPath:ToPath:error:]; and save it in NSUserDefaults and check that flag when your app comes to the foreground again ;)

Hope it helps ;)

like image 101
nacho4d Avatar answered Mar 09 '23 16:03

nacho4d