How do I loop something and wait for something before re-looping? [swift] - swift

Im basically trying to make a segment of code run over and over once they tap once, and run only if the previous run was completed.
How would I do this exactly, Im completely new to Swift.

At the end of your first loop, put a variable that sets to true and then under the IBAction where they tap once, check if that variable is true or not and if it is true, do your re-loop.
Another way you could do it is loop in the viewDidLoad, which is called before any visuals appear. This way, there's no way they could tap the button or whatever you're monitoring until the view has appeared, which is a separate function.
But I doubt you need to wait for a loop to finish, because the execution time of a loop is so fast it will have happened before they can tap (unless it's a very long loop).

1) IBAction ( code would start to run)
2) You could use a if statement to check to see if what you want done is done.
3) You could use a for to determine how long the code is ran.

Related

Are there any ways to loop in a button press callback method until the button is released?

So im using gtkmm specifically here.
What I'm working on:
I'm implementing a feature in which when I press in a certain window a parameter is increased/decreased depending on if the mouse moves left/right.
What needs to be done:
To be able to somehow loop in the button press callback method -(doing so as each iteration would store the new value of the cursor position and compare it to the inital position to determine wheter increase value or decrease)- until the button is released.However the event which is sent remains the same so I cant just for example check to see if the events type becomes GTK_BUTTON_RELEASE. I have also tried to use a flag which is set to false in the button release call back, however the release callback isnt even called as the execution is stuck in the now infinite loop.
Thoughts
Im thinking maybe the solution could be a certain something to do with signals.
Update:
Gtk has two functions for doing what I was saying here exactly which are , gtk_events_pending(), and gtk_main_iteration(). Hever turns out having a limitless loop inside an event is considered a bad practise and its better to use other methods like here for example using GdkEventMotion, or other types.

Swift UIAnimation blocked by for loop

I have a UIAnimation that pulls out a message log, but I need to populate the message log using a for loop as well. I have the animation taking place before the for loop begins, however, the animation doesn't actually begin until after the for loop finishes executing (even though the loop is located after the UIAnimation).
I've tried putting the contents of the for loop within an autoreleasepool (did not work)
I've tried running the for loop on a background thread, however, most of the code within the loop needs to be executed on the main thread, and I haven't had success with that approach.
The for loop is very large, and most of the code is irrelevant to the question, so you can set this up using any UIAnimation, and any relatively time consuming for loop...
for example:
let someView = UIView(frame:CGRect(x:self.view.frame.size.width,y:0,width:self.view.frame.size.width,height:self.view.frame.size.height))
someView.backgroundColor = .red
UIView.animate(withDuration:0.3,animations:{
self.someView.frame.origin.x = 0
})
//You can wrap this with something like:
//DispatchQueue.global(qos: .background).async(execute: {
//But this^ doesn't work on the majority of the code within the loop
for i in 0...1000{
print("hopefully this loop takes a few seconds to finish executing")
print("mainthread tasks being executed")
print("populating UITable in my particular case, and rendering Images/Video/text")
}
The closest that I can get to making it better is to add a "completion" to my UIAnimation, (which will allow the UIView to pull out completely/immediately) but then there is still a second of wait time before the first messages appear...
Also, I am populating a UITableView within the for loop. Not sure if that has any extra relevance to the primary issue I'm having or not...
I'm pretty familiar with DispatchQueue and the differences between async/sync. If you have any potential solutions, or ideas, please let me know.
The animation does not happen immediately, when you call UIView.animate. In stead all the animations are collected and run at some appropriate time. This is why the animation only starts after your for loop is done.
So the way to fix it is to get the animation to run before your for loop runs. Now, accepting what you say, that the for must run on the main thread, you have a number of options. You could put the for loop inside a DispatchQueue.main.async{} or somehow throttle it down so that the animation gets going first. Your idea of using the completion is a good one but you mention some practical issues you picked up with it. So either solve those practical issues, try to run the for inside a DispatchQueue.main.async{} or even, based on practical considerations, start the for loop on the main thread but somewhere else, say in something like didLayoutSubviews (fixed) or as now together with the animation but after a few millis delay to allow the animation to get going.
It's an interesting question. This made me think!
Yes, you mentioned that you understand differences between async/sync but didn't mention if you tried animating in the main thread.
Did you try putting the
UIView.animate(...) in DispatchQueue.main.async{}?

Debugging loops

For some current projects, I'm working with several data structures that are pretty large (in the area of 10K elements). To be able to access this data in lists, I need to use loops and iterators, which can be a pain when the problem area is in the latter half of the list.
So I find myself spending alot of time with my finger on the F8 button in Eclipse's debugger to loop through each element of an iterating loop. This gets worse when have to step through that particular section several times to get an idea why the code is reacting a particular way.
If one has a general idea how many times a loop is to execute before a problem area is hit, is there a way to set a loop breakpoint to execute up to that point then pause?
Use conditional breakpoints.
http://wiki.eclipse.org/FAQ_How_do_I_set_a_conditional_breakpoint%3F
I believe there's a better way to do this, but you can create a trivial block of code in the loop that only executes at a certain iteration, and put the breakpoint inside of it.
if (loopIndex == 1000) {
int number = 14; //Break here
}
Using this as an example:
for(int i=0;i<10000;i++){
System.out.println(i);
}
Set a breakpoint on the print line, then right click on it and select Breakpoint Properties.... From here you can set a condition to trigger the breakpoint. This is the similar to a conditional you would have in an if-statement. If you wanted to trigger the breakpoint when i equals 6000, check the Conditional box and try this:

How to handle class methods being called again before they are finished?

What is the best way to handle this situation on an iPhone device: My program ramps the pitch of a sound between two values. A button pressed calls a method that has a while loop that does the ramping in small increments. It will take some time to finish. In the meantime the user has pressed another button calling the same method. Now I want the loop in the first call to stop and the second to start from the current state. Here is the something like what the method should look like:
-(void)changePitchSample: (float) newPitch{
float oldPitch=channel.pitch;
if (oldPitch>newPitch) {
while (channel.pitch>newPitch) {
channel.pitch = channel.pitch-0.001;
}
}
else if (oldPitch<newPitch) {
while (channel.pitch<newPitch) {
channel.pitch = channel.pitch+0.001;
}
}
}
Now how to best handle the situation where the method is called again? Do I need some kind of mulitthreading? I do not need two processes going at the same time, so it seems there must be some easier solution that I cannot find (being new to this language).
Any help greatly appreciated!
You cannot do this like that. While your loop is running no events will be processed. So if the user pushes the button again nothing will happen before your loop is finished. Also like this you can’t control the speed of your ramp. I’d suggest using a NSTimer. In your changePitchSample: method you store the new pitch somewhere (don’t overwrite the old one) and start a timer that fires once. When the timer fires you increment your pitch and if it is less than the new pitch you restart the timer.
Have a look at NSOperation and the Concurrency Programming Guide. You can first start you operation the increase the pitch and also store the operation object. On the second call you can call [operation cancel] to stop the last operation. Start a second operation to i.e. decrease the pitch and also store the new object.
Btw: What you are doing right now is very bad since you "block the main thread". Calculations that take some time should not be directly executed. You should probably also have a look at NSTimer to make your code independent of the processor speed.
Don't use a while loop; it blocks everything else. Use a timer and a state machine. The timer can call the state machine at the rate at which you want things to change. The state machine can look at the last ramp value and the time of the last button hit (or even an array of UI event times) and decide whether and how much to ramp the volume during the next time step (logic is often just a pile of if and select/case statements if the control algorithm isn't amenable to a nice table). Then the state machine can call the object or routine that handles the actual sound level.

While loop whilst recording - iPhone

What I want to do is have a while statement which loops whilst the recorder.recording property value is set to YES. So, something like:
while (recorder.recording == YES)
{
// Do something here until the record button is pressed again
// Stop the recorder from recording and break out of loop
}
Problem I'm currently facing is that the UI becomes completely frozen due to the while loop. Any ideas?
Either use an NSTimer to perform the action at a given time interval while the recorder is recording or look into using a separate thread to do your loop work on.
Tight loops like this will block the UI if done on the main thread, because that's where the UI is running.