Skip to content

Commit

Permalink
Fix minor issues in README
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeehut committed Oct 18, 2018
1 parent 1d53cd6 commit c3ea519
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
1 change: 0 additions & 1 deletion Demo/Modules/Main/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class MainViewController: UIViewController {
super.viewDidLoad()

view.backgroundColor = viewModel?.backgroundColor

viewModel?.pickedImage.didSet(weak: self) { (self, pickedImage) in
self.pickedImageView.image = pickedImage
}
Expand Down
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,26 @@ class ImagePickerFlowController: FlowController {
}
```

Since the `ResultClosure` parameter is @escaping, you would usually use a `[weak self]` or `[unowned self]` when calling the initializer and then map to something like `strongSelf` to prevent retain cycles & memory leaks. Imperio has a better way to deal with this situation, simply change your code to this:

``` Swift
class ImagePickerFlowController: FlowController {
let resultCompletion: SafeResultClosure<UIImage>

init(resultCompletion: SafeResultClosure<UIImage>) {
self.resultCompletion = resultCompletion
super.init()
}

// ...
}
```

The usage side then would look like this:

``` Swift
func imagePickerStartButtonPressed() {
let resultCompletion = SafeResultClosure<UIImage>(self) { (self, pickedImage) in
let resultCompletion = SafeResultClosure<UIImage>(weak: self) { (self, pickedImage) in
// do something with the result
}

Expand Down Expand Up @@ -314,15 +329,15 @@ override func viewDidLoad() {

view.backgroundColor = viewModel?.backgroundColor

_ = viewModel?.pickedImage.didSet(self) { (self, pickedImage) in
viewModel?.pickedImage.didSet(weak: self) { (self, pickedImage) in
self.pickedImageView.image = pickedImage
}
}
```

The strong `self` which is passed into the `didSet()` is safely returned back as a strong `self` as the parameter of the closure. (It's converted to a weak self automatically by Imperio internally.)

Whenever you want to change the `pickedImage` property, simply use the `setValue()` method of the `ObservablePropertly` like so:
Whenever you want to change the `pickedImage` property, simply use the `setValue()` method of the `ObservableProperty` like so:

``` Swift
mainViewController.viewModel.pickerImage.setValue(pickedImage)
Expand Down

0 comments on commit c3ea519

Please sign in to comment.