Skip to content

Commit 277731c

Browse files
Handle Memory Warning
1 parent 864d1b8 commit 277731c

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

Pod/Classes/ModernSearchBar.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS
7575
setup()
7676
}
7777

78-
7978
private func setup(){
8079
self.delegate = self
8180
self.isSuggestionsViewOpened = false
8281
self.interceptOrientationChange()
8382
self.interceptKeyboardChange()
83+
self.interceptMemoryWarning()
8484
}
8585

8686
private func configureViews(){
@@ -331,6 +331,7 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS
331331
self.addViewToParent(view: self.suggestionsShadow)
332332
self.addViewToParent(view: self.suggestionsView)
333333
self.isSuggestionsViewOpened = true
334+
self.suggestionsView.reloadData()
334335
}
335336
}
336337
}
@@ -429,6 +430,18 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS
429430
// UTILS
430431
// --------------------------------
431432

433+
private func clearCacheOfList(){
434+
///Clearing cache
435+
for suggestionItem in self.suggestionListWithUrl {
436+
suggestionItem.imgCache = nil
437+
}
438+
///Clearing cache
439+
for suggestionItem in self.suggestionListWithUrlFiltred {
440+
suggestionItem.imgCache = nil
441+
}
442+
self.suggestionsView.reloadData()
443+
}
444+
432445
private func addViewToParent(view: UIView){
433446
if let topController = getTopViewController() {
434447
let superView: UIView = topController.view
@@ -479,6 +492,8 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS
479492
}
480493
}
481494

495+
// ---------------
496+
482497
private func interceptKeyboardChange(){
483498
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
484499
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
@@ -498,6 +513,16 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS
498513
self.updateSizeSuggestionsView()
499514
}
500515

516+
// ---------------
517+
518+
private func interceptMemoryWarning(){
519+
NotificationCenter.default.addObserver(self, selector: #selector(didReceiveMemoryWarning(notification:)), name: NSNotification.Name.UIApplicationDidReceiveMemoryWarning, object: nil)
520+
}
521+
522+
@objc private func didReceiveMemoryWarning(notification: NSNotification) {
523+
self.clearCacheOfList()
524+
}
525+
501526
// --------------------------------
502527
// PUBLIC ACCESS
503528
// --------------------------------

0 commit comments

Comments
 (0)