NSObject debounce

A NSObject Category with which we can debounce action on an Object(normal instance or metaclass).

using AssociatedObject to store params been passed with the action.

github: https://github.com/andy380743909/NSObject-Debounce


//
//  NSObject+debounce.h
//  testRoute
//
//  Created by cui panjun on 12-6-14.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface NSObject (debounce)

+(void)debounceForTarget:(id)target action:(SEL)action objectToBeCancelled:(id)oldObject objectToBePerform:(id)newObject delay:(NSTimeInterval)delay;

+(void)debounceForTarget:(id)target action:(SEL)action object:(id)newObject delay:(NSTimeInterval)delay;

-(void)debounceAction:(SEL)action object:(id)newObject delay:(NSTimeInterval)delay;

@end

//
//  NSObject+debounce.m
//  testRoute
//
//  Created by cui panjun on 12-6-14.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "NSObject+debounce.h"
#import <objc/runtime.h>

@implementation NSObject (debounce)

+(void)debounceForTarget:(id)target action:(SEL)action objectToBeCancelled:(id)oldObject objectToBePerform:(id)newObject delay:(NSTimeInterval)delay{

    NSAssert(target != nil, @"target 不能为nil", nil);
    
    // !!! call this method may cancel other actions ,
    //[NSObject cancelPreviousPerformRequestsWithTarget:target];
    
    [NSObject cancelPreviousPerformRequestsWithTarget:target selector:action object:oldObject];
    
#warning ------
//    [NSObject cancelPreviousPerformRequestsWithTarget:target selector:action object:object];
//  the Argument [object] may be different with last performrequest,if so,the last perform will not be cancelled. 
    [target performSelector:action withObject:newObject afterDelay:delay];
    
}

+(void)debounceForTarget:(id)target action:(SEL)action object:(id)newObject delay:(NSTimeInterval)delay{
    
    if(target == nil || action == nil){ return;}
    
    // NSString *selectorString = NSStringFromSelector(action);
    // id oldObj = objc_getAssociatedObject(target,[selectorString UTF8String]); // WRONG
    id oldObj = objc_getAssociatedObject(target,action);
    //NSLog(@"oldObj:%@",oldObj);
    [NSObject cancelPreviousPerformRequestsWithTarget:target selector:action object:oldObj];
    
    objc_setAssociatedObject(target, action, newObject, OBJC_ASSOCIATION_ASSIGN);
    
    // NSString *selectorString2 = NSStringFromSelector(action);
    // id newObj = objc_getAssociatedObject(target,[selectorString2 UTF8String]);
    
    //id newObj = objc_getAssociatedObject(target,@selector(action));
    //NSLog(@"newObj:%@",newObj);
    [target performSelector:action withObject:newObject afterDelay:delay];
    
}

-(void)debounceAction:(SEL)action object:(id)newObject delay:(NSTimeInterval)delay{
    [[self class] debounceForTarget:self action:action object:newObject delay:delay];
}

@end


================================================================================================
Test Code
================================================================================================


-(void)test{
    /*
    
    -----
       -----
         -----
    
    */
     
    [self debounceActionWithString:@"1"];
    [self performSelector:@selector(debounceActionWithString:) withObject:@"2" afterDelay:0.3];
    [self performSelector:@selector(debounceActionWithString:) withObject:@"3" afterDelay:0.5];
}

-(void)debounceActionWithString:(NSString *)str{
    [self debounceAction:@selector(printString:) object:str delay:0.5f];
}

-(void)printString:(NSString *)str{
    NSLog(@"%@",str);
}


OAuth 2.0

 

http://oauth.net/2/

 

http://tools.ietf.org/pdf/rfc6749.pdf

OAuth 2.0

OAuth 2.0 logoOAuth 2.0 is the next evolution of the OAuth protocol which was originally created in late 2006. OAuth 2.0 focuses on client developer simplicity while providing specific authorization flows for web applications, desktop applications, mobile phones, and living room devices. This specification is being developed within the IETF OAuth WGand is based on the OAuth WRAP proposal.

Questions, suggestions and protocol changes should be discussed on the mailing list.

Reading the spec

The final version of the spec can be found at http://tools.ietf.org/html/rfc6749.