我这里有强大的参考周期吗?

时间:2016-05-12 05:51:31

标签: swift design-patterns

我有一个private void ListDrawer() { regionList = new ArrayList<>(); try { JSONArray jsonMain= new JSONArray(JSONResult); for (int i = 0; i < jsonMain.length(); i++) { JSONObject jsonChild = jsonMain.getJSONObject(i); String id=jsonChild.optString("id"); String name=jsonChild.optString("name"); String description=jsonChild.optString("description"); String base_office=jsonChild.optString("base_office"); regionList.add(new RegionList(id, name, description,base_office)); } } catch (Exception ee) { MyRegionActivity.this.finish(); } ArrayAdapter adapter = new RegionListAdapter(MyRegionActivity.this, R.layout.activity_my_region, regionList); adapter.notifyDataSetChanged(); lv.setAdapter(adapter); ,它是我应用中所有其他视图控制器的子类。

我有一些状态变量必须在所有视图控制器中保持一致,所以我打算编写代码,以便在BaseViewController中来回传递这些状态变量一次。 为此,我提供了一个帮助函数BaseViewController用于向前传递并使用pushStatefulViewControllerWithIdentifier()进行向后传递。

StatePassBackDelegate
  1. 我这里有强大的参考周期吗?
  2. 我应该在这里使用单身模式吗?

1 个答案:

答案 0 :(得分:1)

  1. 我在代码中看不到保留周期。如果您担心某个对象在预期时没有被破坏,您可以记录其解除分配以进行检查:

    deinit {
        print("deinit of \(self)")
    }
    
  2. 同意,您需要为状态关怀引入一些模型级实体,而不是在控制器层上使用它。如果您不打算通过单元测试覆盖相关逻辑,单例就可以了。

  3. <强> EDITED

    您可以在app delegate中存储共享对象。

    不确定我是否完全了解您的应用对象模型。我假设应该在所有应用程序中共享状态,因此最好将一些SystemStateMachine注入所有感兴趣的组件。希望它有所帮助:

        @UIApplicationMain
        class MyAppDelegate: UIResponder, UIApplicationDelegate {
    
            var controllersFactory: IViewControllersFactory! = nil
    
            func applicationDidFinishLaunching(application: UIApplication) {
    
                let storyboard = UIStoryboard(name:NSBundle.mainBundle().infoDictionary!["UIMainStoryboardFile"] as! String , bundle: NSBundle.mainBundle())
                controllersFactory = ViewControllersFactory(storyboard: storyboard, stateMachine: SystemStateMachine())
            }
        }
    
        struct SystemState : OptionSetType {
            let rawValue: Int
    
            static let None   = SystemState(rawValue: 0)
            static let Connected = SystemState(rawValue: 1 << 0)
            static let LoggedIn  = SystemState(rawValue: 1 << 1)
    
        }
    
        protocol ISystemStateMachine {
    
            var currentState: SystemState { get set }
        }
    
        class SystemStateMachine: ISystemStateMachine {
    
            var currentState: SystemState = .None
        }
    
        protocol IViewControllersFactory {
    
            func instantiateViewController(identifier: String) -> BaseViewController?
        }
    
        class ViewControllersFactory: IViewControllersFactory {
    
            let _storyboard: UIStoryboard
            let _stateMachine: ISystemStateMachine
    
            init(storyboard: UIStoryboard, stateMachine: ISystemStateMachine) {
                _storyboard = storyboard
                _stateMachine = stateMachine
            }
    
            func instantiateViewController(identifier: String) -> BaseViewController? {
                if let vc = _storyboard.instantiateViewControllerWithIdentifier(identifier) as? BaseViewController {
                    vc.stateMachine = _stateMachine
                    return vc
                }
                return nil
            }
        }
    
        class BaseViewController: UIViewController {
    
            var stateMachine: ISystemStateMachine!
    
            // MARK: Lifecycle
            func somethingWorthHappen() {
                //switch state
                stateMachine.currentState.insert(.Connected)
            }
            func somethingWorth2Happen() {
                //switch state
                stateMachine.currentState.insert(.LoggedIn)
    
                guard let appDelegate = UIApplication.sharedApplication().delegate as? MyAppDelegate else {
                    //log an error
                    return
                }
                if let vc = appDelegate.controllersFactory.instantiateViewController("myViewController") {
                    navigationController!.pushViewController(vc, animated: true)
                }
            }
        }