Need a delay to wait for GPS - objective-c

Using the iPhone and objective C, is there a way to stall or perform a timing loop to allow for the GPS to catch up and return a valid set of coordinates?
Currently, the application runs too quickly and the GPS cannot supply the coordinates fast enough...

Since you said you're on iPhone, you're using CLLocationManager. Just set a delegate on the manager and wait for the locationManager:didUpdateToLocation:fromLocation: message to know when the GPS data is ready.

Assuming your GPS polling is running in a different thread to the User Interface, you can call the static NSThread functions sleepForTimeInterval or sleepUntilDate from the thread that is waiting for the GPS data.

If your mobile application is using GPS, your application should be prepared for location updates, even if your application doesn't track movements..
A common case would be where the user put your application in background and activate it later on a completely different location.
On iOS, create an implementation of CLLocationManagerDelegate like Anomie wrote. And use the timestamp of the update to evaluate the freshness of the location.
Don't sleep & poll like other people suggested.

Either block to wait for data or don't update anything if no data received. There is of course usleep(), but without showing code and specifically how your loop is executed and by what mechanism (threaded or not) we can only answer in general terms.

Related

Slow response when writing value to characteristic for BLE (Objective C)

I'm trying to update a characteristic on a BLE device using Objective C and CoreBluetooth. It works (the write causes a LED to turn on on the device, so I can see when the update occurs), but the delay between writing the value and the response on the device is around 15-25 seconds (!).
I am writing the value using:
[peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
and I get the callback for didWriteValueForCharacteristic 15+ seconds later.
I have noticed that this seems to be something to do with the way I am storing the CBPeripheral object. I think this because if I try to write to the device directly from one of the callback functions in CBPeripheralDelegate (e.g. didDiscoverCharacteristicsForService), which passes the peripheral as an argument, then the response from the device is instant. The long delays only occur if I try to write values to the peripheral using my stored version.
I have tried storing the peripheral as a property, in an array and I have tried performing the write via a queue using dispatch_async (as the action to write the value comes from a user interface action in a C++ program).
Do you have any thoughts or suggestions as to why I am experiencing these problems?
Thanks,
Adam
p.s. the device I am trying to write to is a micro:bit

GPUImage gpus_ReturnNotPermittedKillClient crash using GPUImageFilter

I'm using GPUImageFilter in a chain, and most of the time it works OK. I've recently come across a few random crashes that match the symptoms in this github issue (albeit I'm using GPUImageFilter not live capture or video). I'm trying to find a suitable method that can ensure I've cleared the frame buffer and any other GPUImage-related activities in willResignActive.
Currently I have:
[[GPUImageContext sharedFramebufferCache] purgeAllUnassignedFramebuffers];
Is this sufficient? Should I use something else instead/in addition to?
As indicated there, seeing gpus_ReturnNotPermittedKillClient in a stack trace almost always is due to OpenGL ES operations being performed while your application is in the background or is just about to go to the background.
To deal with this, you need to guarantee that all GPUImage-related work is finished before your application heads to the background. You'll want to listen for delegate notifications that your application is heading to the background, and make sure all processing is complete before that delegate callback exits. The suggestion there by henryl is one way to ensure this. Add the following near the end of your delegate callback:
runSynchronouslyOnVideoProcessingQueue(^{
// Do some operation
});
What that will do is inject a synchronous block into the video processing pipeline (which runs on a background queue). Your delegate callback will block the main thread at that point until this block has a chance to execute, guaranteeing that all processing blocks before it have finished. That will make sure all pending operations are done (assuming you don't add new ones) before your application heads to the background.
There is a slight chance of this introducing a deadlock in your application, but I don't think any of my code in the processing pipeline calls back into the main queue. You might want to watch out for that, because if I do still have something in there that does that, this will lock your application. That internal code would need to be fixed if so.

iOS 7 - Is there really a way to do reliable polling via background fetch without Push Messages?

I have an application where I need to create local notifications via polling without doing push - primarily due to client infrastructure limitations and their security model.
I've read: http://www.objc.io/issue-5/multitasking.html, I've seen David Chan's WWDC presentation - where single push messages kick off download tasks - but what I truly need is background fetch - on a regular basis - like every ten minutes - in iOS 7.
I've seen the VOIP hacks. No. What non-hack way is there to do this without user interaction or push messages? Any examples you can point me to?
Here's what I know:
Background data tasks will work in the debugger but if you can get a console on an IPA, you'll quickly find out they really are prohibited (thereby invalidated many examples).
Background URL tasks require custom delegates - but fetch completion handlers are iffy. This too I found with an IPA and console.
I would love to avoid using the AFNetworking lib - for something quite simple.
Background fetch is not a reliable solution - you are at the mercy of the OS, and it is not very merciful. Abusing iOS background modes is not a reliable solution - Apple is known to reject applications that enable background modes, such as location, VOIP and music playback, without a legitimate reason. Background URL tasks are not something you can rely upon to wake your app; they will wake it, but the app will not be awake enough in the background to enqueue a background URL task.
Your best and most intended method is still background fetch, but be prepared to be disappointed. Your app will not be woken app in the interval you need. Also, the user can kill the app in the app switcher screen, causing your app to never wake up until opened.
No real reliable method other than push. You need to insist with your client for the sake of user experience.
Unfortunately there is no "reliable" way to do that on iOS. With the background fetch API you are not guaranteed to have process run when you would like it run. As you've said, you've already looked at the API so i'm probably telling you something that you already know. A local notification wouldn't solve your issue either as this isn't a way that you can wake your application up and kick off network events. This is behavior that Apple doesn't want as this would negate the whole purpose of their background task coalescing.
You really need to have a push mechanism in place for something like this, so if this is something that is needed, then you may have to stress that to the client.

Desing pattern for background working app

I have created a web-service app and i want to populate my view controllers according to the response i fetch(via GET) in main thread. But i want to create a scheduled timer which will go and control my server, if there becomes any difference(let's say if the count of an array has changed) i will create a local notification. As far as i read from here and some google results, i cant run my app in background more then ten minutes expect from some special situations(Audio, Vo-IP, GPS).. But i need to control the server at least one per minute.. Can anyone offer some idea-or link please?
EDIT
I will not sell the app in store, just for a local area network. Let's say, from the server i will send some text messages to the users and if a new message comes, the count of messages array will increment, in this situation i will create a notification. I need to keep this 'controlling' routing alive forever, whether in foreground or background. Does GCD give such a solution do anyone have any idea?
Just simply play a mute audio file in loop in the background, OR, ping the user's location in the background. Yes, that will drain the battery a bit, but it's a simple hack for in-home applications. Just remember to enable the background types in your Info.plist!
Note: "[...] I fetch (via GET) in main thread." This is not a good approach. You should never fetch any network resources on the main thread. Why? Because your GUI, which is maintained by the main thread, will become unresponsive whenever a fetch isn't instantaneous. Any lag spike on the network results in a less than desirable user experience.
Answer: Aside from the listed special situations, you can't run background apps. The way I see it:
Don't put the app in the background. (crappy solution)
Try putting another "entity" between the app and the "server". I don't know why you "need to control the server at least one per minute" but perhaps you can delegate this "control" to another process outside the device?
.
iOS app -> some form of proxy server -> server which requires
"babysitting" every minute.

detect non responsive windows in mac os x

I am developping an objective c application and I would like to detect non responsives windows even if they are not own by my application.
Is there a way to be notified when a such case occurs?
Thanks in advance for your help,
Regards,
I think the only way to detect whether an window is hanging is to detect when its application is hanging. And I think the only way to - reliably - do this is to talk to it. Send it some inter-process message and await an action. I think that's exactly how the system detects it: there is some delay before the beach ball appears. And this is because the system sent a message and received no answer in x seconds.
What kind of message that might be is hard to say. Must be something that goes through the main event loop but can be sent by every application. I'm sure Google will be of some help finding it. I'm no pro in inter-process communications and would have to search as well.
You can use the Instruments application with a "Spin Monitor" instrument track. If you set it to monitor "All Processes", it will capture stack traces whenever an application hangs (doesn't process the main event loop for a long time).