zoukankan      html  css  js  c++  java
  • 关于MVC模型的NSNotification用法

    对于iOS开发开发者, Model View Controller 模型能帮你快速理清开发思路,最近在使用Model给Controller传递数据时候了解了关于

    NSNotification的一些用法,写出来记录一下,代码片段如下:

    当PhotoDataBaseContext被更新时候,会广播一个通知,通知一般都是NSDictionary类,包含了一个Key和Value,Value是发送内容的地址;

    接受消息的一方必须要在发起广播之前开始监听,因为广播是一瞬间的事情,当错过了广播的时间之后就再也接受不到通知了,所以一般Controller会在绘制xib开始建立监听,那么就用到 -(void)awakeFromNib,抢在广播之前建立监听,这样就可以在广播的时候收到通知,从而得到更新,因为接收到的数据的地址,所以以后在改地址被更新的数据在Controller内也能被更新。欢迎大家给一下不同的建议;

    -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    
        self.photoDataBaseContext  = [self createMainQueueManagedObjectContext];
        [self startFickrFetch];
    
    
    - (void) setPhotoDataBaseContext:(NSManagedObjectContext *)photoDataBaseContext{
        
        [NSTimer scheduledTimerWithTimeInterval:10*60 target:self selector:@selector(startFickrFetch:) userInfo:nil repeats:YES];//Call startFlickrFetch:
        
        
        _photoDataBaseContext = photoDataBaseContext;
        NSDictionary * userInfo = self.photoDataBaseContext ? @{ PhotoDatabaseAvailabilityContext : self.photoDataBaseContext } :nil;
        //当发送通知时候,被通知的对象必须先于发送通知之前建立监听信息
        //因为发送通知时候,通知在发送之后,如果没有被监听,那么就再也接受不通知,
        //所以要被通知者要优先建立监听,否则机会丢失信息
        [[NSNotificationCenter defaultCenter] postNotificationName:PhotoDatabaseAvailabilityNotification object:self userInfo:userInfo];
    }
    //Call startFlickrFetch:
    - (void) startFickrFetch:(NSTimer*)time{
        [self startFickrFetch];
    }
    // 写在控制器代码内 ViewController
    -(void)awakeFromNib{ NSLog(@"notification"); [[NSNotificationCenter defaultCenter] addObserverForName:PhotoDatabaseAvailabilityNotification object:nil queue:nil usingBlock:^(NSNotification *not){ self.manageObjectContext = not.userInfo[PhotoDatabaseAvailabilityContext]; }]; }
  • 相关阅读:
    zend guard 4/5 破解版和免过期办法,已补授权Key一枚,成功注册。
    一身冷汗,PHP应该禁用的函数
    CentOS 5.5 安装和卸载桌面
    php加速模块 eAccelerator open_basedir错误解决办法
    配置电信网通双线双IP的解决办法
    php安装igbinary模块
    ubuntu系统VNC服务器安装配置
    python3 之 闭包实例解析 Be
    python3 之 内置函数enumerate Be
    python3 之 匿名函数 Be
  • 原文地址:https://www.cnblogs.com/zuopeng/p/4062135.html
Copyright © 2011-2022 走看看