How to keep checking a value until it changes without using infinite loop?

Frank Joe

My code needs to check on a sensor value until it becomes 1 or the code reach the time out. Hence I am thinking of such an infinite loop.

The sensor is a singleton object that has access to sensor value at real-time. The default value is 0 and I want to monitor for the value change to 1.

for (;;) {
  int sensorVal = [sensor getVal];
  if (sensorVal == 1) break;
  if (timeout) break;
}

But the problem is that I don't want to have an infinite loop in my code. I am wondering if there is a better way to write this in Objective-C?

Mojtaba Hosseini

You can achieve this with using the KVO (key Value observation).

Observe for changes that you want:

[sensor addObserver:self forKeyPath:@"val" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];//observing the key in this class

Then use this delegate method:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if([keyPath isEqualToString:@"val"]) {
        /* Do the work here */
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python: How to make a infinite loop that will keep counting up until a certain number is reached

How to while loop until the end of a file in Python without checking for empty line?

How to make a loop on code in pynput without using ram and that is infinite

how to scrape using scrapy with infinite loop without next page information

Checking if the status changes using while loop

Keep only first appearance of a value until it changes to another value

How to keep checking for correct input in a loop

How to keep checking array until certain condition is met

Infinite while loop in C when given value and subtracting until zero

For loop checking for cell without empty value and appending

Java program keep infinite loop without any error message

Activating a function when file changes without the need for an infinite loop

How to keep decreasing value in mongo until is 0

Loop until screen changes

how to enter an infinite loop until valid data is input?

How to write FileSystemWatcher without triggering an infinite loop

How to implement this without triggering an infinite loop with useEffect

How to do an infinite loop without blocking CPU

How to call setState without getting an infinite loop?

How to update Api without making an infinite loop?

How to reload a page until it changes using Puppeteer

How to return a value from Infinite Loop?

How could I make an infinite "For" loop able to save values, without using "while"?

How can I fix this code, I keep receiving an infinite loop?

How do I make a loop that commands the user to keep inputing a value until If conditions are met, with the aid of a try catch statement?

how to be aware of database changes without checking from android application

How do I keep a loop going with a variable that changes in every loop

C - keep running into an infinite loop

firestore keep updating in infinite loop

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive