zoukankan      html  css  js  c++  java
  • Chapter 18 Saving, Loading, and Application State

    Chapter 18 Saving, Loading, and Application State

     

    1. Archiving is one of the most common ways  of persisting model objects on iOS. Archiving an object involves recording all of its properties and saving to the filesystem. Unarchiving recreates the object that from the data.

    Classes whose instances need to be archived and unarchived must conform to the NSCoding protocol and implement its two required methods, encodeWithCoder: and initWithCoder:

     

    2.  Below is how archiverRootObject:toFile: works:

    The method begins by creating an instance of NSKeyedArchiver. (NSKeyedArchiver is a concrete subclass of the abstract class NSCoder.)

    privateItems is sent the message encodeWithCoder: and is passed the instance of NSKeyedArchiver as an argument.

     

    3. In order to have objects that are not view controllers respond to low memory warning, you must use the notification center. Every application has an instance of NSNotificationCenter, which works like a smart bulletin board. An object can register as an observer (“Send me ‘lost dog’ notifications”). When another object posts a notification (“I lost my dog”), the notification center forwards the notification to the registered observers. Whenever a low-memory warning occurs, UIApplicationDidReceiveMemoryWarningNotification is posted to the notification center. Objets that want to implement their own low-memory warning handlers can register for this notification.

    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

            [notificationCenter addObserver:self

                                   selector:@selector(clearCaches:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];

     

     

    4. The standard Model-View-Controller design pattern calls for the controller to be bear the burden of saving and loading model objects. However, in practice, this can become overwhelming - the controller is simply too busy handling the interactions between model and view objects to deal with the details of how objects are fetched and saved. Therefore, it is useful to move the logic that deals with where model objects come from and where they are saved to into another type of object: a store.

    A store exposes a number of methods that allow a controller object to fetch and save model objects. The details of where these model objects come from or how they get there is led to the store. The store could access a database, talk to a web service, or use some other method to produce the model objects for the controller.

  • 相关阅读:
    看了支付宝账单,我才知道我是『有钱人』
    用 Pytest+Allure 生成漂亮的 HTML 图形化测试报告
    测试开发城市群,第一站深圳,我们来了!
    测试工作常用 Linux/ Shell 命令之 Bash 逻辑控制基础
    性能测试实战 | JMeter 录制/回放做 App 后端性能压测
    测试圣诞狂欢,万元红包、冲榜豪礼等你拿!
    自动化测试工具 or 自动化策略?孰轻孰重?
    Java 面试题
    疫情之下,普通人高薪就业指南:方向对了,路就不会遥远!
    SharePoint WebPart:扩展SharePoint 2007中图片展示功能
  • 原文地址:https://www.cnblogs.com/1oo1/p/4005447.html
Copyright © 2011-2022 走看看