Unschedule update method in cocos2d-x - ios

I want to unschedule update method after 5 minutes of game start.
I scheduled my update method in constructor of my class.
scheduleUpdate();
I am trying to unschedule my selector by calling stop update method. But it's not unscheduling.
runAction(Sequence::createWithTwoActions(
DelayTime::create(5.0f),
CallFuncN::create(this, callfuncN_selector(GameScene::stopupdate))));
Stopupdate method :-
unscheduleUpdate();

Firstly, DelayTime::create(5.0f) will create delay of 5 secs. Not 5 minutes.
Have you check with some log or etc that code reaches stopUpdate method ?
CallFuncN is used when function accepts CCNode * param.
You should use CallFunc.
And i am guessing, that is this->unsheduleUpdate();
Also, if this all doesn't help,
use schedule() to custom schedule a function.
You can set this to frame rate and on repeat, so it works same as a scheduleUpdate.
unschedule() to unschedule it.

Related

For an SKSprite, is customAction actually the Update call?

Because of general psychosis, Apple put an update() call in SKScene, but they forgot to put an Update call in SKSpriteNode.
Now, as far as all our testing can determine,
In SpriteKit, just using a "customAction" on a sprite seems to be exactly the same as running something in update in the scene.
func teste() {
let a = SKAction.customAction(withDuration: 5.0) { [weak self] node, elapsedTime in
print("Honest to goodness, this is the run loop. I think.")
print("\(self?.k) \(elapsedTime)")
self?.k += 1
}
run(a)
}
I have scoured the documentation to no avail.
Does anyone know if it's really true that "customAction" indeed runs every frame? Is it effectively and safely an Update call?
(Example: conceivably, there could be a horrific coincidence that they run it "every 1/60th" or something, and it's not really running on the same runloop as the scene.)
Or, since apparently it's really just Box2D, maybe someone can shed light on this from the Box2D milieu?
Resolution of the issue:
Thanks to Knight, we now know that using customAction "as the Update call" is almost the same as in the update call: it happens in the second phase of the run loop, which is immediately after the update phase.
{If you prefer to have it actually happen in the update phase, you would need to use the usual workaround in SpriteKit - just call your own update function in the game objects, from, the Update call apple provide in the scene.}
All action happen in the action phase of the update cycle
See https://developer.apple.com/documentation/spritekit/skscene

Execute method after response

I am stuck in one interesting problem as i want to execute a method after 3 second but also want to check whether i got response of that method or not.
If i not got the response in 3 second then that method will not be executed.
Using code:
Timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector:#selector(receiveMessageWebservice)userInfo: nil repeats:YES];
This will run receiveMessageWebservice in 3 second of interval. But problem is sometime response not come in 3 second it take more time then i got two or three response same time.
I don't want so.
Please help.
When you request for the Web Service just invalidate the timer and recreate it in the response success/failure block.
In this way you can successfully run the service request after every fixed interval without requesting multiple times
Definitely, network calls cannot be time guaranteed . As i assume , you want to retry for a response till it doesn't come to you.
As i feel you should not use timer here, instead rely on web service completion and failure block to reinitiate the re-fetch of web service. If the response comes in failure block , then only retry.

Swift time between run function

I am nearly new to swift Xcode and I am building an app, when the end user is near a iBeacon hi will get a local push notification.The problem I have is each time he comes near to it(if he got back and forward he will get each time he is near).
So I think to limit by time like 5 minuets of some like that.
I can not find in Swift how to limit a function to run in a time limit.(like 5 minutes)
Can some one point me in the correct direction?
Thanks for the help.
I did try to work with a timer but it did not do the job for me.
You Can create a Variable: beaconsHasBeenRecognized to turn to true when the beacon has been recognized, then the next time user goes back and forth, before triggering notification, your code should evaluate it beaconsHasBeenRecognized,its false, otherwise, if it is true, it will not trigger the notification.
Then with the timer, at the moment you set beaconsHasBeenRecognized to true, you start a timer to change beaconsHasBeenRecognized to false within the time that you want, like the 5 minutes.

UIApplicationDidEnterBackgroundNotification remaining time for execution

In my app using iOS 9.2, Swift 2.1 I need to save some data into core data when the app goes to background. For this I registered each of the view controllers in the call path for UIApplicationDidEnterBackgroundNotification notification, with an instance method each for saving respective data.
I read on multiple places that by default the app gets about 5 seconds to finish off the execution and hence we need to use beginBackgroundTaskWithExpirationHandler to extend it to about 5 minutes. Following is an example of the selector method that responds to the above notification.
func applicationEntersBackground()
{
print("Before Extension: \(UIApplication.sharedApplication().backgroundTimeRemaining)")
let taskID = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler(nil)
print("During Extension: \(UIApplication.sharedApplication().backgroundTimeRemaining)")
saveCoreData()
if(taskID != UIBackgroundTaskInvalid)
{
UIApplication.sharedApplication().endBackgroundTask(taskID)
}
print("After Extension: \(UIApplication.sharedApplication().backgroundTimeRemaining)")
}
Following is the results of print() statements
Before Extension: 179.933103708318
During Extension: 179.930266333336
After Extension: 179.922843541659
My doubts are
Why is the remaining time about 180 seconds even before I requested for time extension? I tried multiple times. It is always close to 180 seconds and not the 5 seconds as suggested.
Why doesn't the call to beginBackgroundTaskWithExpirationHandler have any impact on the remaining time?
Once the applicationEntersBackground method of a VC returns, similar notification is sent to another VC's corresponding method. Suppose 180 seconds is the total extended duration and VC1 spends about 10 seconds on notification handling, does VC2 notification handler get around 170 seconds between its beginBackgroundTaskWithExpirationHandler - endBackgroundTask calls?
Between successive invocations of the notification handlers of different VCs, there is obviously a very short period where the extension request is not active. How does the timing play out in this case? Does the 5 second counter (provided it is true) come back to life as soon as an endBackgroundTask call is made, and possibly terminate the application before the next VC can get its notification?
Appreciate any help.
By looking at the documentation for backgroundTimeRemaining:
While the app is running in the foreground, the value in this property remains suitably large. If the app starts one or more long-running tasks using the beginBackgroundTaskWithExpirationHandler: method and then transitions to the background, the value of this property is adjusted to reflect the amount of time the app has left to run.
To answer your questions:
backgroundTimeRemaining stays around 180 while the application is in foreground so you can tell what time you'd have once you start a background task. This value is not an indicator of how long are you allowed to run without a background task.
beginBackgroundTaskWithExpirationHandler has an impact, as you can see, the remaining time decreased (by a small value as the method doesn't take much time)
What matters here is the time passed between the call to beginBackgroundTaskWithExpirationHandler and the one to endBackgroundTask. You can split whatever you need the time interval between your calls, providing you don't exceed the 180s limit
Once you call endBackgroundTask the application will be suspended, regardless it took 2 seconds or 179 seconds.
You can find out more details about application entering background here. I'd recommend going through the documentation, it might clarify other questions you might have on this matter.

Cocos2d (iOS): scheduleOnce called in callback won't fire

I am scheduling a callback via scheduleOnce (Cocos 1.1b), and when the callback is executed and once all tasks were performed there, I try to reschedule the same callback again (just with a different delay). The reasoning is to achieve a varying delay between the callbacks.
However, while it is called properly the first time, the second scheduling will never fire it again. Stepping through the Cocos libs, it eventually adds a timer to the list, but it won't fire.
Any clue what I am doing wrong and need to do differently?
Edit: just saw this entry in the log on the second scheduling:
CCScheduler#scheduleSelector. Selector already scheduled. Updating interval from: 0.00 to 0.00
I tried now to unschedule all timers explicitly first, however it doesn't make a difference. I would anyway expect scheduleOnce to reset that timer on callback.
It may be a bug in Cocos2D, after all you're using the very latest beta version. So I won't divulge into that, you may want to report this through the official channels however (cocos2d forum, google code issues for cocos2d-iphone).
In the meantime you can simply do this:
-(id) init
{
…
[self scheduleSelector:#selector(repeat) interval:0];
}
-(void) repeat
{
// simply schedule the selector again with a new interval
[self scheduleSelector:#selector(repeat) interval:CCRANDOM_0_1()];
}
Alternatively, if you want to re-schedule the selector at a later time, you can unschedule it as follows within the repeat method (the _cmd is shorthand for the selector of the current method):
-(void) repeat
{
[self unschedule:_cmd];
// re-schedule repeat at a later time
}

Resources