Adding item to array once when function is called multiple times?

jcad

I want to add a channel to an array of channels if its level is above a threshold, this function is the delegate callback for providing the channel level.

This fucn is used constantly to provide level data, however On init i want to add the channels above the threshold to an array. However then i dont want it to add them again, just 1 instance of each channel that meets the criteria.

I wanted to use this code and check if the channel i receive data from is already in the channelArray, if it isnt, then add it and if it is then skip over it...However its not calling / working.

Can anyone help me with the approach to this? Issue is each channel calls this func every time its moved, so i need it to just run the code to add to array a single time.

- (void)cdcControlDidReceiveBusSend:(NSInteger)channel withValue:(float)value forBus:(NSInteger)bus onModule:(NSInteger)module {
    NSNumber *recievingChannel = [NSNumber numberWithInteger:channel];
    NSLog(@"RECIEVED SEND DATA FOR CHAN:%ld VALUE:%f FORBUS:%ld", (long)channel, value, (long)bus);
    if (value != -80.000000) {
        NSLog(@"CHANNEL:%ld FADER IS UP ATTEMPTING TO ADD TO FOCUS ARRAY", (long)channel);

        for (NSNumber *arrayChannel in self.focusChannels) {
            if (recievingChannel == arrayChannel) {
                NSLog(@"ALREADY SAVED THIS CHANNEL...SKIPPING");
            } else {
                NSLog(@"ADDING CHANNEL %ld", (long)channel);
                [self.focusChannels addObject:[NSNumber numberWithInteger:channel]]; // add the channel number to the array of channel numbers if the fader is up
                NSLog(@"FOCUS ARRAY NOW CONTAINS %lu CHANNELS", (unsigned long)self.focusChannels.count);
            }
        }

    } else {
        NSLog(@"CHANNEL:%ld FADER IS DOWN NOT IN MIXFOCUS ARRAY", (long)channel);
    }
}
toma

You can not use == operator to compare values of NSNumber. In order to do things right you ned to use comparator method, like isEqueal

if ([recievingChannel isEqual:arrayChannel]) {
    //...
}

or compare values directly

if (recievingChannel.integerValue == arrayChannel.integerValue){
    //...
}

Anyway, this task can be solved using -[NSArray containsObject:]

- (void)cdcControlDidReceiveBusSend:(NSInteger)channel withValue:(float)value forBus:(NSInteger)bus onModule:(NSInteger)module {
    if (value != -80.000000) {
        if ([self.focusChannels containsObject:@(channel)] == NO) {
            [self.focusChannels addObject:@(channel)];
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP: When an associative array is within a function, and the function is called more than once, does it create the array multiple times?

From Dev

Function is run multiple times, but only called once

From Dev

setTimeout function issue when called multiple times

From Dev

scrollTop jumps when called multiple times on same item

From Dev

Async function works when called once but not multiple occasions

From Dev

Why is this jQuery function failing when called on multiple times?

From Dev

Why is this function called multiple times?

From Dev

c# event handler is called multiple times when event is raised once

From Dev

Function in function will not be called multiple times if requested?

From Dev

Scroll Function Firing Multiple Times Instead of Once

From Dev

Cloud Function running multiple times instead of once

From Dev

I have to run a function just once when after user stops typing but my function runs multiple times

From Dev

function adding space to array item

From Dev

function adding space to array item

From Dev

Why is a default value set for a Python 3 function overwritten when the function gets called multiple times?

From Dev

function called multiple times from template

From Dev

javascript event function being called multiple times

From Dev

Controller function gets called multiple times

From Dev

Cakephp Callback function called multiple times

From Dev

why is the scope function called multiple times?

From Dev

Scope function is getting called multiple times in AngularJs

From Dev

Postgres: STABLE function called multiple times on constant

From Dev

single function in javascript called multiple times

From Dev

javascript event function being called multiple times

From Dev

function called multiple times from template

From Dev

GSP template called once but rendered multiple times on UI

From Dev

GSP template called once but rendered multiple times on UI

From Dev

function being called n+1 times instead of once -- why?

From Dev

Ruby, adding multiple objects to an array at once

Related Related

  1. 1

    PHP: When an associative array is within a function, and the function is called more than once, does it create the array multiple times?

  2. 2

    Function is run multiple times, but only called once

  3. 3

    setTimeout function issue when called multiple times

  4. 4

    scrollTop jumps when called multiple times on same item

  5. 5

    Async function works when called once but not multiple occasions

  6. 6

    Why is this jQuery function failing when called on multiple times?

  7. 7

    Why is this function called multiple times?

  8. 8

    c# event handler is called multiple times when event is raised once

  9. 9

    Function in function will not be called multiple times if requested?

  10. 10

    Scroll Function Firing Multiple Times Instead of Once

  11. 11

    Cloud Function running multiple times instead of once

  12. 12

    I have to run a function just once when after user stops typing but my function runs multiple times

  13. 13

    function adding space to array item

  14. 14

    function adding space to array item

  15. 15

    Why is a default value set for a Python 3 function overwritten when the function gets called multiple times?

  16. 16

    function called multiple times from template

  17. 17

    javascript event function being called multiple times

  18. 18

    Controller function gets called multiple times

  19. 19

    Cakephp Callback function called multiple times

  20. 20

    why is the scope function called multiple times?

  21. 21

    Scope function is getting called multiple times in AngularJs

  22. 22

    Postgres: STABLE function called multiple times on constant

  23. 23

    single function in javascript called multiple times

  24. 24

    javascript event function being called multiple times

  25. 25

    function called multiple times from template

  26. 26

    GSP template called once but rendered multiple times on UI

  27. 27

    GSP template called once but rendered multiple times on UI

  28. 28

    function being called n+1 times instead of once -- why?

  29. 29

    Ruby, adding multiple objects to an array at once

HotTag

Archive