@@ -10,59 +10,48 @@ import RealmSwift
1010
1111class DownloadsManager {
1212
13- private var realm : Realm !
14-
15- //private var downloads: Results<SongDownload>!
16-
17- var queue = DispatchQueue ( label: " realm " )
13+ private let realmQueue = DispatchQueue ( label: " com.MusicPlayer.realmQueue " , qos: . userInteractive, attributes: . concurrent)
1814
1915 private var downloads : Results < SongDownload > {
20-
16+ return try ! Realm ( ) . objects ( SongDownload . self ) . sorted ( byKeyPath : " creationDate " , ascending : false )
2117 }
2218
2319 var downloadsCount : Int {
2420 return try ! Realm ( ) . objects ( SongDownload . self) . count
2521 }
2622
27- init ( ) {
28- queue = DispatchQueue ( label : " realm " )
29- queue . async {
30- Realm . asyncOpen ( configuration : Realm . Configuration . defaultConfiguration ) { realm, error in
31- if let realm = realm {
32- self . realm = realm
33- self . downloads = self . realm . objects ( SongDownload . self ) . sorted ( byKeyPath : " creationDate " )
34- // Realm successfully opened, with migration applied on background thread
35- } else if let error = error {
36- // Handle error that occurred while opening the Realm
23+ func addDownload ( _ download : SongDownload , completion : @escaping ( ) -> ( ) ) {
24+ realmQueue . async {
25+ autoreleasepool {
26+ let realm = try ! Realm ( )
27+ try ! realm. write {
28+ realm. add ( download )
29+ }
30+ realm . refresh ( )
31+ DispatchQueue . main . async {
32+ completion ( )
3733 }
3834 }
39- //self.realm = try! Realm()
40- // self.downloads = self.realm.objects(SongDownload.self).sorted(byKeyPath: "creationDate")
4135 }
4236 }
4337
44- func addDownload( _ download: SongDownload ) {
45- queue. async {
46- autoreleasepool {
47- try ! self . realm. write {
48- self . realm. add ( download)
49- }
50- self . realm. refresh ( )
38+ func removeDownload( _ download: SongDownload , completion: @escaping ( ) -> ( ) ) {
39+ let downloadRef = ThreadSafeReference ( to: download)
40+ realmQueue. async {
41+ let realm = try ! Realm ( )
42+ guard let download = realm. resolve ( downloadRef) else {
43+ return
5144 }
52- }
53- }
54-
55- func removeDownload( _ download: SongDownload ) {
56- queue. async {
57- try ! self . realm. write {
58- self . realm. delete ( download)
45+ try ! realm. write {
46+ realm. delete ( download)
5947 }
60- self . realm. refresh ( )
48+ realm. refresh ( )
49+ completion ( )
6150 }
6251 }
6352
6453 func download( for index: Int , completion: @escaping ( SongDownload ) -> ( ) ) {
65- queue . async {
54+ realmQueue . async {
6655 let download = self . downloads [ index]
6756 let downloadRef = ThreadSafeReference ( to: download)
6857 DispatchQueue . main. async {
@@ -76,7 +65,7 @@ class DownloadsManager {
7665 }
7766
7867 func download( with url: URL , completion: @escaping ( SongDownload ? ) -> ( ) ) {
79- queue . async {
68+ realmQueue . async {
8069 guard let download = self . download ( with: url) else {
8170 DispatchQueue . main. async {
8271 completion ( nil )
@@ -95,7 +84,7 @@ class DownloadsManager {
9584 }
9685
9786 func indexForDownload( with url: URL , completion: @escaping ( Int ? ) -> ( ) ) {
98- queue . async {
87+ realmQueue . async {
9988 let index = self . downloads. index {
10089 $0. url == url
10190 }
@@ -106,26 +95,20 @@ class DownloadsManager {
10695 }
10796
10897 func setupStatus( _ status: DownloadStatus , forDownloadWith url: URL ) {
109- queue . async {
98+ realmQueue . async {
11099 guard let download = self . download ( with: url) else {
111100 return
112101 }
113- //try! self.realm.write {
114- download. status = status
115- //}
116- self . realm. refresh ( )
102+ download. status = status
117103 }
118104 }
119105
120106 func setupProgress( _ progress: Progress , forDownloadWith url: URL ) {
121- queue . async {
107+ realmQueue . async {
122108 guard let download = self . download ( with: url) else {
123109 return
124110 }
125- //try! self.realm.write {
126- download. progress = progress
127- //}
128- self . realm. refresh ( )
111+ download. progress = progress
129112 }
130113 }
131114
0 commit comments