poniedziałek, 19 stycznia 2015

CoreLocation limitations - how to overcome them ?

Let's say you have 100 items at your small store and you want to get notified in background every time you're in a proximity of each on an iOS app. The answer seems easy, right ? Let's use iBeacons! After some time you finally get to the final conclusion: iBeacons are useless for your shop.

CoreLocation sucks 

Don't get me wrong, CoreLocation is a very good high level library but it's limitations make iBeacons useless in some scenarios. Why ? You can listen to max 20 combinations of beacon UUID / Major / Minor. If you put 100 beacons close to each other with the same UUID, and monitor only for that UUID (major = any, minor = any) you will get only one didEnterRegion callback. You can then start ranging (listening to all beacons), but you can do that only while the app is in foreground.

Apple explains in it's documentation that they do that to limit the OS resources that the apps use. 

"Regions are a shared system resource, and the total number of regions available systemwide is limited. For this reason, Core Location limits to 20 the number of regions that may be simultaneously monitored by a single app. "

UUID has 128 bits, major and minor have 16 bits each. That's 160 bits for every beacon we monitor (there's also an identifier string but let's pretend it doesn't exist).

That's 3200 bits per app, if we assume that 20 is maximum we can monitor for. That's around 0.000381 megabytes. Let's get crazy and crank that maximum up to 800 beacons per app. It would take something like 0.01524 of a megabyte to monitor for 800 beacons at one time! Let's get insane crazy and assume that 100 apps we have installed are monitoring for 800 different beacons.

100 apps monitoring 800 beacons each would take 1.524 megabytes of RAM. I'm not an OS specialist but I think it's not the end of the world, especially that it would be a challenge to find 100 apps like these.

Of course, there's a tremendous challenge of finding the app that is monitoring for this particular beacon while stumbling on any iBeacon. You have to loop through all the 100 apps with 800 beacons, right ? Or you can just be a genius like me and use a hash map.

Why does Apple do this? Probably to not allow waking your app up too often. 

Hack through Apple

How to overcome this issue ? I've thought a lot about this. I've found a solution of beacon clusters: beacons with the same majors. When you enter a cluster, you start listening to all the minors inside. It would take a lot of planning around the store.

There's a better solution here that takes a little planning, but not as much as in my solution.

Just program your beacons to have 20 different UUIDs and make sure the same UUID don't overlap. It's like a puzzle.

Use CoreBluetooth

Or you can just use your own Bluetooth LE packet and use CoreBluetooth. CoreBluetooth lets you do all that CoreLocation does, but without any limits. You just need a special permission (bluetooth-central in *.plist) to discover devices in background.