Why NSNotification work slow? - swift

I know its interesting title for my question. Sorry for that. Here is my problem;
I am doing simple login screen (with facebook frameworks). If user wants to login with his/her facebook account, firstly he/she must confirm facebook page. And then i get their email addres, name, surname..etc information of user.
After that i just ask them password for my app. This password screen is a view. and opening on main view(not as new viewcontroller. its view on main view) And after confirm facebook page then this wiew appear.
On this password view has 2 textbox and 2 button. textbox for paswword(and confirmation) and First button send request to my web service with user information. and other button is "cancel" button.
here is the problem. When user send request i create nsnotification for waiting respond. after getting respond. i want to this password view get hide.
here is the code:
#IBAction func buttonSave(sender: AnyObject) {
var myObj = connectObject()
myObj.sendRequest("http://localhost:8888/iphone/hearMe/index.php", param: "id=test123")
NSNotificationCenter.defaultCenter().addObserver(self, selector: "actOnSpecialNotification", name: "sendDataCheck", object: nil)
}
func actOnSpecialNotification() {
println("ok I got success respond from webservice")
self.viewPassword.hidden = true /*This is my view */
}
with this code. "viewPassword" view hided but not instantly. It hide after 10 or 15 seconds. How i can hide that view instantly after getting response from webservice?

It's not clear from your question whether the 10-15 seconds is between the pushing of the button and the hiding of the view, or between the printing of "ok I got…" and the hiding of the view.
If the lag is between pushing the button and calling actOnSpecialNotification(), then the above code doesn't help us. We need to know what object posts the notification sendDataCheck and why it takes so long. Maybe it just takes that long to talk to the network. Maybe it posts the notification before you start observing it. You probably want to call addObserver() before sending the request rather than after (just in case there's an async operation in there).
If the lag is between printing the "ok I go…" line and hiding the view, then the most common cause of that is that the notification was posted on a background queue. You can't interact with UIKit anywhere but the main queue, and weird lags are a common symptom when you do.
(Side note: when in doubt, use let, not var. myObj is better defined let here. This helps protect against many kinds of bugs.)

Related

WKInterfaceLabel.setText not reflecting changes following modal viewController dismissal

I am working on a simple video game for the Apple Watch. In awakeWithContext() I call my own method, startNewGame(), and register for it to be called again when receiving a notification from my modal viewController. This all works fine. Then upon determining the game is over, I call self.presentControllerWithName().
When that controller is dismissed by the player I call self.dismissController(). Then I fire a notification that once again calls startNewGame(). This is where things get weird.
self.score = 0
let scoreString = formatScore(0) //"0000"
self.scoreboard.setText(scoreString)
let hiScore = NSUserDefaults.standardUserDefaults().integerForKey("hiScore")
let hiScoreString = formatScore(hiScore)
self.hiScoreboard.setText(hiScoreString)
The above excerpt from startNewGame() shows me resetting the score and updating both "scoreboards" both WKInterfaceLabels that present scores in a skeumorphic old-timey LCD fashion. Because of this, I call formatScore() which returns a string with leading zeros. Anyway, I then set the text on them both and… nothing happens. Both boards show the same score as before the game over view controller was shown. It is only when they are next updated in response to player's actions that they update to reflect the correct values. Because I only have issues with this when the code runs shortly following the dismissal of a modal viewController, I suspect there is some connection. Anyway, I am stumped, some help would be much appreciated.
I ran into the same problem receiving data from the iPhone using sendMessage.
My code was pretty straight forward:
dispatch_async(dispatch_get_main_queue(),{
if let error = error {
NSLog("showing error: \(error)");
self.lblTitle.setText("Error...");
}
});
For me, the message was being logged, but the interface wasn't getting updated.
Ends up I was showing multiple pages using reloadRootControllersWithNames and when I tried to update a label within a page that wasn't being shown, the update was ignored.
I fixed it by listening to willActivate and didDeactivate to see whether to update the label or whether to save the text so I could apply it when the page is shown.
Apparently this is by design.

Multiple simultaneous UILocalNotifications

If I have two (or more) UILcalNotifications that fire more or less on the same time and the application is active in the background, I found out that:
Two alerts are shown simultaneously to the user, one covering the other.
When the user touches "View" on the top alert, the alert is removed from the screen, didReceiveLocalNotification is called for this notification, and the application enters the foreground.
As soon as the top alert is removed from the screen (and the application is already in the foreground), the user sees the alert that was under it.
However, if the user touches "View" for this alert as well, nothing happens. didReceiveLocalNotification is not called for the second notification, and the application has no way of knowing that the user wanted to view this notification as well.
If the application happens to be in the foreground when the two notifications fire, there's no problem - didReceiveLocalNotification is called for both, one after the other (no alert is shown in this case).
Is there a way to get some notification for both "View" confirmations in the above case? Am I doing something wrong?
Actually the previous notification do not call the didReceiveNotification delegate method (or any other method if two or more notification pops while app in background). But you can track that previous notification as your need.
For example, if you have a app that send data to the server on clicking view of notification then on daily basis save the data into a plist (and edit it daily) that data has been sent or not by "Yes" or "No" so while a notification comes the app checks into the plist that the data for previous alarms have been sent or not. If no then it will send it at that time. So this is just an single way.
Alert view needs to have it's delegate set (typically to self) to receive events.

Dismissing a UIAlertView

I put an in app purchase into my app, and when the user taps a button, the purchase is started. So basically, they tap the button, and then depending on the speed on their Internet connection, they could be waiting for up to ten seconds until a new alert view comes up asking if they would like to buy the product. The user will probably tap the button multiple times since nothing came up, and then multiple purchase alert views will come up. Additionally, this could maybe be seen by the user as an app bug. In the end, this is a problem.
I want an alert view to come up with a spinning wheel that says "Loading..." when the users taps the buy button. Now my problem is, how do I get that to dismiss when the new alert view comes up asking the user if they want to buy the product?
if ([UIAlertView alloc] that says: #"whatever Apple's alert view says")
{
//dismiss the "Loading..." alert view here
}
I doubt that would work, so any input is appreciated. Thanks!
You need to have access to that alertview. You can do this. Create a alertview instance var in app delegate and when you want to show loading initialize that instance var assign to your property and when you want to dismiss just call
[alertViewinstance dismissWithClickedButtonAtIndex:0];
Write this piece of code in a method in appDelegate. Hope you get the idea. If not let me know I'll post the sample code here.

Deferentiating the push notification handler when application is foreground and background

It is said that (correct me if I'm wrong) if the application is in the foreground we have to handle push notifications in the "didReceiveRemoteNotification" and if the application is in the background using "didFinishLaunchingWithOptions" when user taps the "view" button of the app. As I dont have a phone to test I want to know whether I am handling this properly.
1) What will be invoked when I taps on the "View" button in the push notification?
2) Let say I am running the application in the foreground and push notification receives at the same time. Will I be given the push notification alert? If so what will happen if the user click on the View button?
3) In this thread How to handle push notifications if the application is already running? it says:
"alert" key will not be there directly under the userInfo dictionary, you need to get another dictionary with name "aps" and then get the "alert" or "body" from "aps" dictionary"
Is this true?
4) I need to push to a certain view when the user clicks on the View button. Hence do I need to handle that code in both methods?
Thank you
There's a nice rundown of the methods invoked by a push notification in this Apple vid: http://developer.apple.com/videos/iphone/#video-advanced-pushnotification - make sure you visit download the full version in iTunes.
This direct link might work: http://developer.apple.com/itunes/?destination=adc.apple.com.3391495696.03391495702.3416205190?i=1378617410
Either way, the general idea is that if your app isn't in the foreground, tapping your view button will trigger didFinishLaunchingWithOptions, and if it is the foreground app, you'll get the didReceiveRemoteNotification.
I don't think you'll get the alert. The method didReceiveRemoteNotification will be called, and it'll be up to you to show a UIAlert if you want.
Yes - that's true.
Yes, but I think you can simplify this by creating a third method specifically designed to handle your view. You can call this from both didFinishLaunching (only if it launched via a notification), and didReceiveRemoteNotification. This way, if your app needs to be launched, you can have time to do any other setup you might need to do for the app to work right out of the get-go (load saved data, init tabbar controllers or anything else like that).
Best of luck

Data Formatters temporarily unavailable

I'm currently working on an iPhone app.
This app has a login screen, also a signup screen.
After the user has successfully signed up, I dismiss the signup view, then the app automatically logs in using the created account. After which, the login view is dismissed, showing the main view.
I'm trying to modify this by immediately dismissing the login view, since I already have the account details of the user when the signup is successful. Basically, the ideal flow is: after the user successfully signs up, I save the username and password in a singleton class, then dismiss the signup view. When I get to the parent view (which is the login screen), I have a variable that checks if there was a successful signup. If that variable is true, I want to immediately dismiss the login view.
However, I come across this error message: Data Formatters temporarily unavailable, will re-try after a 'continue'. (Unknown error loading shared library "/Developer/usr/lib/libXcodeDebuggerSupport.dylib")
I'm not really sure why this happens. I have no problems dismissing the login view when I go through the actual login procedure - which of course also dismisses the login view if the user inputs a correct username and password.
I'm not exactly sure, but I'm starting to think that the iPhone cannot handle dismissing 2 view controllers almost at the same time.
Is it possible that I'm dismissing the login view too quickly? Is that a factor? Is there anyway for me to be able to dismiss 2 view controllers almost simultaneously without coming across this error message?
It seems likely that dismissing 2 UIViewControllers at the same time is the cause of this error (I have seen it for various other reasons, including running short of memory).
Try a different flow, where you check for the saved values first, then load your main view if valid, or the login view if not. I do this in one app, and it works fine.