Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5994979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:53:44+00:00 2026-05-22T23:53:44+00:00

I’m looking for best practices to solve this hard problem: My app has a

  • 0

I’m looking for best practices to solve this hard problem:

My app has a file import / export functionality coupled with Core Data. This means that when I import a file into my app, I create an entry in Core Data and copy this file over to a private directory of the App. Next, I process the file and create a few others out of it for different purposes in the app. These are heavy calculations taking about 0.2 seconds for every file on an iPhone 3G.

This means that I am dealing with some kind of “transactions” while importing. It’s all done using one big FileImportOperation : NSOperation which iterates over all files that have to be imported, and performs the heavy import work on every single file in the list.

The problem is this: Background threads ans NSOperations get killed by the OS in just a nanosecond without prior warning. Only the Main thread gets up to 5 seconds time to quickly save some work and quit.

How can I ensure that I never end up importing a file half-way, leaving the app in a corrupted or trashed state?

From my point of view, the evil problem is that background threads (including NSOperation) get killed right away when the user quits the app, without any chance to clean up. I tried to compensate this by writing the managed object only if all those files have been created and saved successfully (remember: every imported file is duplicated and processed several times in different ways for different things). But I can still end up with an incomplete import, trashing the disk of the user.

My temporary solution to the problem looks like this: The Object that owns the NSOperationQueue registers for the UIApplicationWillTerminateNotification notification. When it receives it, sends -cancelAllOperations to the queue, which flags all the running operations as cancelled.

At this point I’m stuck. Now my NSOperations are flagged as cancelled, but they still have to complete their current internal run loop and stop.

My next idea is to force the Main thread to wait until all the Operations are completed, and then let -applicationWillTerminate: return.

I’m hoping for some pro-advice on how to implement secure transactions to prevent leaving the app in a corrupted state when importing in the background.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:53:45+00:00Added an answer on May 22, 2026 at 11:53 pm

    Can you perform your import operation in a manner that, if not atomic, is at least restartable? Eg. a file move that is restartable might look like:

    Search for and delete all files ‘*.tmp’ in target folder.
    Find a file in source folder that needs to be copied – found one ‘data.DB’
    Copy file to target folder as ‘data.DB.tmp’
    Rename ‘data.DB.tmp’ to ‘data.DB’

    On app start, search for and delete all files ‘*.tmp’

    Coupled with a transaction-based ‘Core Data’ DB update, (ie. can be rolled back), can this kind of approach solve your problem?

    Rgds,
    Martin

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm looking for suggestions for debugging... If you view this site in Firefox or
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.