# Chapter 15: Challenge 2 - Persisting the filter between app launches

**URL:** <https://forums.kodeco.com/t/chapter-15-challenge-2-persisting-the-filter-between-app-launches/104973>\
**Category:** Combine: Asynchronous Programming with Swift\
**Created:** [April 11, 2020, 7:09pm UTC](https://forums.kodeco.com/t/chapter-15-challenge-2-persisting-the-filter-between-app-launches/104973 "2020-04-11T19:09:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![taufi](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/taufi/32/105896_2.png) [@taufi](https://forums.kodeco.com/u/taufi)\
**Post date:** [April 11, 2020, 7:09pm UTC](https://forums.kodeco.com/t/chapter-15-challenge-2-persisting-the-filter-between-app-launches/104973/1 "2020-04-11T19:09:04Z")

</div>

Persisting doesn’t work because didSet in Settings.keyworks is never called. Does anyone has an idea what goes wrong?

---

<div class="post-metadata">

**Author:** ![miibpa](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/miibpa/32/95017_2.png) [@miibpa](https://forums.kodeco.com/u/miibpa)\
**Post date:** [April 13, 2020, 11:36am UTC](https://forums.kodeco.com/t/chapter-15-challenge-2-persisting-the-filter-between-app-launches/104973/2 "2020-04-13T11:36:04Z")

</div>

Apparently the line `self.settings.keywords.append(new)` in SettingsView is not triggering the `didSet` in the `keywords` property inside `Settings` model

---

<div class="post-metadata">

**Author:** ![taufi](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/taufi/32/105896_2.png) [@taufi](https://forums.kodeco.com/u/taufi)\
**Post date:** [April 13, 2020, 11:59am UTC](https://forums.kodeco.com/t/chapter-15-challenge-2-persisting-the-filter-between-app-launches/104973/3 "2020-04-13T11:59:05Z")

</div>

Thanks miibpa,

do you think it’s a Swift bug?

taufi

---

<div class="post-metadata">

**Author:** ![miibpa](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/miibpa/32/95017_2.png) [@miibpa](https://forums.kodeco.com/u/miibpa)\
**Post date:** [April 13, 2020, 3:27pm UTC](https://forums.kodeco.com/t/chapter-15-challenge-2-persisting-the-filter-between-app-launches/104973/4 "2020-04-13T15:27:18Z")

</div>

Yep It seem to be a bug in Swift 5.2 ☹

> <https://github.com/apple/swift/issues/54525>
>
> | | |
> |------------------|-----------------|…
> |Previous ID | SR-12089 |
> |Radar | rdar://problem/58999150 |
> |Original Reporter | lindsey89 (JIRA User) |
> |Type | Bug |
> |Status | Resolved |
> |Resolution | Done |
>         
> 
> Attachment: \[Download\](https://user-images.githubusercontent.com/2727770/164963618-a67d893f-ad5c-47f9-9830-54724f9b69a5.gz)
> 
>       
> \<details\>
> \<summary\>Environment\</summary\>
> 
> Xcode Version 11.2.1 (11B500)
> 
> Toolchain: Swift 5.2 Snapshot 2020-01-25 (a)
> 
> 
> \</details\>
> 
> \<details\>
> \<summary\>Additional Detail from JIRA\</summary\>
> 
> | | |
> |------------------|-----------------|
> |Votes | 3 |
> |Component/s | Compiler |
> |Labels | Bug, PropertyWrappers |
> |Assignee | None |
> |Priority | Medium |
> 
>    
> 
> md5: 21c46ed73b44e201c5901af9e79428cc
> 
> \</details\>
> 
> 
> 
> 
> \*\*is duplicated by\*\*:  
> \* \[SR-12350\](https://bugs.swift.org/browse/SR-12350) Projected PropertyWrappers don't trigger didSet when their wrappedValue is mutated  
> \* \[SR-12407\](https://bugs.swift.org/browse/SR-12407) Calling \`.toggle\` on a wrapped boolean no longer calls didSet  
> 
> \*\*relates to\*\*:  
> \* \[SR-12178\](https://bugs.swift.org/browse/SR-12178) willSet not firing when used with a property wrapper  
> 
> 
> \*\*Issue Description:\*\*
> 
> 
> Whilst running the Swift 5.2 toolchain 2020-01-25 I noticed that didSet did not get called correctly on a propertyWrapper backed property. 
> 
> I've included an example project to illustrate the issue. 
> 
> \*\*\<u\>Reproduction steps\</u\>\*\*
> 
> \- navigate to contentView.swift
> 
> \- add a breakpoint to line 24
> 
> \- run the app in simulator 
> 
> \- add new expense by pressing the + button
> 
> \- fill in the form 
> 
> \- click save 
> 
> \- notice that the breakpoint isn't called
> 
> \- this is not the case with included swift in xcode

---

<div class="post-metadata">

**Author:** ![afrank](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/afrank/32/110937_2.png) [@afrank](https://forums.kodeco.com/u/afrank)\
**Post date:** [May 2, 2020, 8:48pm UTC](https://forums.kodeco.com/t/chapter-15-challenge-2-persisting-the-filter-between-app-launches/104973/5 "2020-05-02T20:48:46Z")

</div>

Read on ‘stackoverflow’ that structs like ‘FilterKeywords’ aren’t KVO compatible. The value has to be a subclass of NSObject using @objc dynamic properties. So adding a value in the array won’t trigger ‘willSet/didSet’

My solution was to use a temporary array instead and replace the whole array in the ‘AddKeywordView’ closure which triggered the ‘didSet’ method as expected.

**//Reassign array to trigger ‘didSet’**  
let saveKeywords = self.settings.keywords + [new]  
self.settings.keywords = saveKeywords

Another possible way is to use the underlying ‘wrappedValue’ of the binding property, maybe a hack…

self.$settings.keywords.wrappedValue = new

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [July 4, 2020, 3:18am UTC](https://forums.kodeco.com/t/chapter-15-challenge-2-persisting-the-filter-between-app-launches/104973/6 "2020-07-04T03:18:29Z")

</div>

@afrank Thank you for sharing your solution - much appreciated!
