Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Question about dispatch_timer function
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Question about dispatch_timer function



Hi there,

I've a question about the dispatch_timer function. I try to make a strobe flash on my iPhone app and for that i used an NSTimer but a friend of mine say that it's better to use dispatch function. So i was looking and try to implement something. I explain my code before post anything :
I have a UISlider in another view (settings.strobeValue) and get his value for settings the timer. And i called changeStrobeState in another function who wll be called by dispatch function. But when i hit the button on my view and send my dispatch code, no work at all... So there is the code :

-(void)changeStrobeState
{
    if (NO == [[NSUserDefaults standardUserDefaults] boolForKey:@"buttonStrobeState"])
    {
        [deviceCapture lockForConfiguration:nil];
        [deviceCapture setTorchMode:AVCaptureTorchModeOff];
        [deviceCapture unlockForConfiguration];
    }
    else
    {
        [deviceCapture lockForConfiguration:nil];
        [deviceCapture setTorchMode:AVCaptureTorchModeOn];
        [deviceCapture unlockForConfiguration];
    }
    BOOL state = [[NSUserDefaults standardUserDefaults] boolForKey:@"buttonStrobeState"];
    [[NSUserDefaults standardUserDefaults] setBool:!state forKey:@"buttonStrobeState"];

}


- (dispatch_source_t)setupStrobeTimer {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
         _strobeTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
        if (_strobeTimer) {
            dispatch_source_set_timer(_strobeTimer, dispatch_walltime(DISPATCH_TIME_NOW, settings.strobeValue * NSEC_PER_SEC),
                                      settings.strobeValue * NSEC_PER_SEC, 6ull * NSEC_PER_SEC);
            __block ViewController *blockSelf = self;
            dispatch_source_set_event_handler(_strobeTimer, ^{
                [blockSelf changeStrobeState];
            });
        }
    });
    return _strobeTimer;
}

Can you help me ?

Thanks for reading and sorry 'cause my english is not perfect and it's actually 3 AM here :)

Cheers,
Vavelin Kevin
Twitter | Blog | Viadeo

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.