0

I would like to save data to a file every second. The data records is simple, just a date and number, and would like to append to the file. This can last the whole day, so the file can end up being big.

I do not want to block the main thread, nor have concurrency issues. It also has to be capable to being written to in the background, as opposed to the app being in the foreground.

What's the best way forward to achieve this?

TruMan1
  • 33,665
  • 59
  • 184
  • 335
  • On iOS? Why are you doing that / planning to? – Wain Jul 03 '16 at 20:02
  • For persisting streaming heart rates from a bluetooth device, then I need to create visualization graphs from the data. It has to work offline and whether the app is in the foreground or the background. – TruMan1 Jul 03 '16 at 20:03

2 Answers2

1

You could use Core Data for this. Saving one item per second is not going to impact your app performance at all. Using Core Data will give you ability to query the data, so if you need to show chart from 10 AM to 11 AM from you 24 hours of data for example, it won't be a problem.

Background data collection capability depends on the data source. For example, you can collect location updates in the background. The system provides tools to make this energy efficient. Check http://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

MirekE
  • 11,515
  • 5
  • 35
  • 28
  • Is this overkill since it's only a number and date per record? They both require I/O except Core Data has a mountain of overhead it seems. – TruMan1 Jul 03 '16 at 20:40
-1

From the documentation:

The bluetooth-central Background Execution Mode

[...] While your app is in the background you can still discover and connect to peripherals, and explore and interact with peripheral data. In addition, the system wakes up your app when any of the CBCentralManagerDelegate or CBPeripheralDelegate delegate methods are invoked, allowing your app to handle important central role events, such as when a connection is established or torn down, when a peripheral sends updated characteristic values, and when a central manager’s state changes. [...]

To use it, just go to your Project Settings > Your Target > Capabilites, enable Background Modes and check "Uses Bluetooth LE accessories"

Community
  • 1
  • 1
Kametrixom
  • 14,673
  • 7
  • 45
  • 62