Wednesday, November 9, 2016

How to use barrier

Example:
https://github.com/lingzt/BarrierPra
By using the barrier, I can make sure every time getter starting after the previous setter finished.


#import "ViewController.h"

NSString *_name;
@interface ViewController ()
@property (nonatomic, strong, readwrite) NSString *name;@property (nonnull, strong, nonatomic) dispatch_queue_t syncQueue;@end
@implementation ViewController- (void)viewDidLoad {

    [super viewDidLoad];    //并行队列   set up on global queue    _syncQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);    for(int i=0; i<1000; i++){
        NSString *tempName = [NSString stringWithFormat:@"%d",i];        [self name];        [self setName:tempName];
    }

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}




//用同步  sync with global queue- (NSString *)name{
    __block NSString *localName;    dispatch_sync(_syncQueue, ^{
        localName =_name;        NSLog(@"getter");        NSLog(localName);    });    return localName;}
//利用异步栅栏块  async with global que, with barrier- (void)setName:(NSString *)name{
    dispatch_barrier_async(_syncQueue, ^{
        _name = name;        NSLog(@"settter");        NSLog(_name);    });}


@end

Monday, November 7, 2016

Effective Objective-C 2.0 study notes

in Chinese, great stuff!
http://www.cnblogs.com/purple-sweet-pottoes/p/4742552.html

http://blog.csdn.net/sanjunsheng/article/details/44371341

difference bw category & extension

Category:
1. The category .h file is optional. When it is deleted the .m file have to import the main .h file
2.  To expose the method, it has to be declared in the .h file.
3. Can't declare any new property in the category.

Extension:
1. Kinda like category,  but anonymous, what is even better,  it can declare the new property!
2. Can be used to expose read-only properties.
for example:

in main .m file, declare the internal method

@interface Account ()

@property (nonatomic, readonly) NSString* randomStuff;
+ (void)setCurrentAccount:(Account*)account;

then implement the method
+ (void)setCurrentAccount:(Account*)account
{
    currentAccount = account;}


in other class's .m file, set extension interface to expose the method to the current class.


@interface Account ()
+ (void)setCurrentAccount:(Account*)account;@end

later in this class, we will be able to call the private method.

- (void)cleanCurrentUser{
    [Account setCurrentAccount:nil];
}

Sunday, November 6, 2016

ReactiveCocoa

I have found some senior people heavily using ReactiveCocoa in the project I am currently working on. So, knowing ReactiveCocoa https://github.com/ReactiveCocoa/ReactiveCocoa is my homework of this Sunday.

Following are some material I am following today:







Study note:
It is 1:30am and I really got to go to bed. That way I will have enough energy for pairing with others tomorrow.

It was not as productive as I expected, so so far I found RAC has something to do with MVVM, which is super cool.
More reading to come: https://www.objc.io/issues/13-architecture/mvvm/