Hello!
- Launch app
- Select Album
- Go to background
- Terminate app in Xcode
- Run app
- Error:
Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘An instance 0x127d0f3b0 of class UIImageView was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x1700390c0> (
<NSKeyValueObservance 0x17004ba30: Observer: 0x1742608c0, Key path: image, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, Property: 0x17004be20>
)’
Possible is deinit missed in AlbumView?
deinit {
valueObservation.invalidate()
}
@dia-doca I’ve followed your steps but I’m not able to replicate the issue. In the meantime I will try to investigate. Thanks!
@lorenzoboaro Do you have any feedback on all of this? Thank you - much appreciated! :]
If the app goes to the background, it does restore the current album alright.
However if you dump the app from the app stack altogether, and re-launch it, then the first Bowie album is the ‘current’, not the one that was selected when you left the app.
Thanks for the heads up @lorenzoboaro can you please help with this when you get a chance?
Hi @rommex73! Thanks for your feedback! Saving the current album happens only when you put the app in the background. If you kill the app, you do not give the chance to the operative system to save the state of the app. Let me know for anything! Lorenzo
TAKE IT AS A CONSTRUCTIVE CRITIQUE:
Sorry but I hate this kind of tutorial where there’re tons of lines of code just to copy and paste.
This tutorial is about iOS Desing patterns which is great and, after some time, Design pattern’s tutorial get in pause and become a tutorial for a scroll view (which is great too!), then back to Design Pattern. It’s very messy because people have to stop focusing on a topic then pass to a secondary unexpected argument and lose his focus on the main topic and, to finish quickly, secondary topic is very neglected with just short and incomplete explanation.
You might say “just copy and paste scroll view’s code!”, I don’t like it! I like to re-type the code to understand it but, if I’m studying another topic, it’s hard!
Every time I meet this kind of thing I get angry and just close everything but it’s sad.
For example, in this case, you might offer a ready HorizontalScrollerView.swift just to drag in the project and go on then make another tutorial exclusively for that, where everything is explained better.
I mean, every code in your site is precious but, treated in this way, become useless and unproductive.
1 Like
Hi @matte.car! Your feedback it’s really appreciated. And I’m also sorry that you feel sad. Right now what I can do is to take into account your critique for future ones. Thank you very much :] Have an amazing day, Lorenzo
Grazie Lorenzo! It’s not the first time it happens and it’s pretty frustrating, would be wonderful if it will prevented in future!
Even because are two very interesting topics but, treated in this way, it’s hard to understand deeply both…
Thanks @lorenzoboaro. I see your point but there is a strange thing. The app restores its current album even without the code under the Memento pattern. That is, I uncomment all the code that is relevant to State Restoration from the final project, and the state is still preserved.
So how about that?
Thank you @rommex73!
If you comment the state restoration from the final project it behaves as expected. You can test it doing these steps:
- select an album different from the first one
- put the app in background
- kill the application from Xcode
- run the application again
Now the selected album should be different from the one you selected in step 1.
If the application remains in background, and not killed, obviously this will not happen since the app remains in a frozen state.
If you perform the same steps as above with state restoration code uncommented the behaviour will be different. In particular, the app will remember the latest selected album.
Hi @lorenzoboaro. Sorry for this prolonged discussion but I really want to get to the point.
I’m testing on a real device and I put myself in place of a real user (he’s got no XCode).
So, the user selects the U2 album and presses Home button. Then he double clicks Home button and re-enters the app. U2 is selected still.
Then the user double clicks Home button, swipes up the app (kills it) and re-enters the app. Bowie is selected.
This happens in both variants of the app, with and without the Memento code. I may be blind to something obvious but I feel puzzled…
Hello @rommex73. I guess I’ve discovered the problem. Sorry, it was my fault. You should use the following restoration mechanism in ViewController
. I will update the zip in the tutorial as well.
//MARK: State restoration
extension ViewController {
override func encodeRestorableState(with coder: NSCoder) {
coder.encode(currentAlbumIndex, forKey: Constants.IndexRestorationKey)
super.encodeRestorableState(with: coder)
}
override func decodeRestorableState(with coder: NSCoder) {
currentAlbumIndex = coder.decodeInteger(forKey: Constants.IndexRestorationKey)
super.decodeRestorableState(with: coder)
}
override func applicationFinishedRestoringState() {
showDataForAlbum(at: currentAlbumIndex)
horizontalScrollerView.reload()
}
}
In particular, super.decodeRestorableState(with: coder)
should be called after retrieving the previous currentAlbumIndex
. For the sake of completeness, I’ve also overridden applicationFinishedRestoringState
. Now the logic to show the album data and reload the scroller view is separated.
Are you able to give me a feedback? Thanks, Lorenzo
Hi @lorenzoboaro, thanks for the swift reply )
Addition of super… () is great, but my point was different. I was pointing out that the state preservation happens whether or not the code is present.
I’m a novice so my questions could be dumb, sorry…
UPD I did update the code, it works fine as it used to do
@rommex73, thanks for your feedback! Right now the state restoration should not happen in both cases. Only when the code for restoring is present. Are you able to confirm this?
And not worry, there are no dumb questions :] I’m happy, whenever possible, to help.
@lorenzoboaro After all the Memento code is uncommented and restoration IDs deleted from the storyboard the state restoration still works…
@rommex73 In order to have a clean working test, you can try to delete the app.
For the record, I agree 100% with matte.car. A tutorial about Design Patterns shouldn’t have so much space devoted to creating a carousel (which I prefer to build with a horizontal UICollectionView anyway ).
I’d say for future tutorials, find examples that are as simple as possible so they don’t distract from the main point, which in this case is the Adapter pattern (I think).
1 Like
100% agree! Just quited this tutorial at the middle of doing stuff with scroll bar. Was it really necessary for the article purpose? I don’t think so… Aren’t there more clear and simple way to demonstrate how does design patterns work in real life in couple lines of code? Definitely they are… Sorry for this opinion, but it’s true
1 Like