Function runs without waiting for previous one to finish - iphone

I have a problem with a function running before the previous one has had time to finish. According to the logs they both run but it is almost as if the function called from the AppDelegate is put onto a separate thread.
-(IBAction) checkLocation {
[AppDelegate locateMe];
[self checkForChangeAndReloadTable];
}
The locateMe function can be called from several views which would do different things with the information. It finds the user's location and saves it to user defaults. checkForChangeAndReloadTable checks the defaults and, if the location has changed, reloads the table with new, more relevant information. It seems the change happens after it checks so this does work if the checkLocation function is called twice by the user.
Is this expected? Any ideas how to get around it?

Without seeing any source code to the locateMe or checkForChangeAndReloadTable methods, it's impossible to say, but several CoreLocation methods run asynchronously, which is probably your problem.

you can use notifications to handle asynchronous events. The basic idea is to set a notification that calls checkForChangeAndReloadTable when locateme completes

Related

Where can I found documentation about page lifecycle events?

I am using cdp (https://github.com/mafredri/cdp) in order to use devtools protocol to generate a pdf of a page. But first I need to know when the page is completely loaded. I found that the networkIdle event can help me to know when this occurs. But, I have troubles because the networkIdle event sometimes fired twice. Then I need to know when this one is fired
There are two parts for what you're looking for.
First of all, the reason the event is fired twice. When a new tab (target) is created, the first page it loads is about:blank. You get lifecycle events for this page as well. The second time the load event is fired is the one you're looking for (if you're using Page.lifecycleEvent).
Now, to handle the second matter - there are also other events you can use. The basic one for page loading is Page.loadEventFired, which, as far as I recall, will only be fired for the actual page (but I could be wrong about this one).
Important note: If you're using lifecycle events, they are fired for each frame separately, meaning that the main frame might finish loading before the sub frames are loaded. Page.loadEventFired has a different behavior and waits for all frames to fire their load event.
Here is a good article on the page lifecycle api.
Another possible solution could be:
document.onreadystatechange = function () {
if (document.readyState === 'complete') {
run the screenshot code...
}
}

Finish activity onPause but still be in backstack?

I'm trying to minimize memory usage in my app, and one of the things I'm doing is calling finish() in the onPause method (which I know is not the best way to do things). For the most part, it seems to be working well, but when the user clicks the back button from the next activity, it logically skips over the finished activity and goes back further. Is it possible to have that activity in the back stack and just get recreated if the user presses back?
No. This conclusion comes from the task and backstack documentation as well as the activity documentation and a general understanding of how a stack data structure works.
A stack data strucure only has 2 possible operations push/put, which adds something to the collection, and pop, which removes it. Stacks folow a last in first out model, or LIFO, where by last thing added - in your case an activity - is the first thing removed when pop is called.
Within the android lifecycle activities are generally popped from the stack when the back button is pressed. At that point onDestroy() is called and the activity is removed (you can verify this by overriding the onDestroy() method and logging the results if you want to check). Alternativly you can force onDestroy() to be called by calling finish() as you are. Finishing an activity effectivly does the same thing as pressing back. The activity is destroyed and must be recreated before it can be added to the stack.
For what you're trying to do the stack would have to incorporate some intermediate state in which an activity does not exist but rather something akin to a reference is held that, when moved to the top, would indicate that the corresponding activity should be recreated. Since this is not how the sack works - it only holds activities - that state cannont exist and so the result you are talking about is not possible.
Your Goal is to minimize memory usage,Just make use of activity life cycle, You can do this alternative(if you need)
-Just leave onCreate() method blank.(only do setContentView(layout))
-Override onResume();
-whatever you were doing in onCreate just copy paste to onResume().
-and In onPause(), Recycle your all bitmaps and set them to null(I think you are using Bitmaps thats why you are very cautious about it ). and remove your views.
Now what will happen, when you launch your new activity, onPause() would be called. that will remove your all bitmap and views. and when you come back, onResume() will be call.(onCreate will not be called). and that will again initialize your view and bitmaps.
No, i don't think that is possible. Once you finish the Activity it's gone. You could, however, implement and handle your own stack. On back pressed, you would just start the closed Activity again.

Robotium : Is there a way to check for an activity to NOT exist?

I'm automating an app that shows some overlay messages anywhere on the app for several scenarios, such as app installed for the first time etc. (I'm fairly new to Robotium too.)
The overlay displays a text that goes away by swiping or clicking on it. Also, there are different types of these overlays with different unique text on it. (let's call it Activity A)
I wanted to create a robust test case that handles this case gracefully. From the test's perspective we won't know that the activity A will be present all the time. But I want to recover from the scenario if it does, by writing a method that I can call any time. Currently, the tearDown method gets called since my expected activity name doesn't match.
Also, even if the activity A exists, there are other predefined overlay texts too. So, if I use solo.waitForText("abc") to check for text "abc", I may see the overlay 2 with the text "pqr" instead.
So I was looking for a way to automate this, and I can't use solo.assertCurrentActivity() or solo.waitForActivity methods as they just stop the execution after the first failure.
So any guidance is appreciated!
All the waitFor methods return a boolean. So you can use waitForActivity() exactly as you want to. If the Activity doesn't exist it will return false.
You can check which Activity is current:
Activity current = solo.getCurrentActivity();

See what methods are being called in Xcode

I have a view controller that keeps reloading over and over again. I would like to be able to see which methods are being called so that I can see the one that keeps firing the reload. I know I can see what was called if an error occurs and the app crashes. However, in this case there is no crash. Is there a way to monitor all of the methods being called throughout the app?
If you are new to XCode and Objective C and looking for something lightweight and you do not have a large code/ many methods, I would put:
NSLog(#"%s",__PRETTY_FUNCTION__);
in every method.
Use Instruments. Start your code in a profiling mode and select the CPU time instrument. As the app runs, Instruments will gather information about every call stack at regular intervals, which will allow you to see what calls what. (Use the "Invert Call Tree" option to see callers of a given function.)
I use this macro:
#define DEBUG 1
#if DEBUG
# define NLog(fmt, ...) printf("%s\n", [[NSString stringWithFormat:#"%s:%d %#", __PRETTY_FUNCTION__, __LINE__, [NSString stringWithFormat:fmt, ##__VA_ARGS__]] UTF8String])
#else
# define NLog(...)
#endif
Then I include my Macros.h in my Application_Prefix.pch file, so that it's available everywhere. Before I ship, I set DEBUG to 0 so that all NLogs disappear.
So now, instead of using NSLog, I use NLog. The output looks something like this:
-[ApplicationDelegate applicationDidBecomeActive:]:86 applicationDidBecomeActive called!
This solution was based on these earlier posts:
How to print out the method name and line number and conditionally disable NSLog?
Do I need to disable NSLog before release Application?
You can just place the NLog calls in several places, to see which functions are called right before your view controller reloads. If you need to know the exact function that triggered the reload, your best bet would be to place a breakpoint and examine the call stack, as others have mentioned.

Xcode/iPhone -- break when the next event enters my code?

I am working on a large (>30k lines) event-driven app. I have a sequence of inputs that produces a bug. What I want to do is to break as soon as the final input enters my code.
Is there a general way to do that?
I understand that for any specific sequence of inputs, I can find out where that last input is going to enter my code, then set a breakpoint there. What I would like to do is take out the step of "find out where that last input enters my code." In other words, I am running the app in the simulator, and I want to set a flag somewhere that says "break the next time you are going to enter non-system Objective C code." Then I send the event that causes the problem.
I understand what you are asking, but have you tried using an Exception Breakpoint? This will basically act like an auto-inserted breakpoint on the piece of code that throws the exception. If that doesn't work for you, try a symbolic breakpoint
If you want to intercept UI events, you can try subclassing UIWindow and overriding its sendEvent: method, then setting this class as the class of the UIWindow object in your main XIB file. sendEvent: will be called each time the user generates a touch event. Unfortunately, at this point you cannot yet know which UI object will finally consume the event (read: which event handler code will be ultimately called) since that depends on the actual state of the responder chain. But anyway, you can use this method to inject events into the system.