Skip to content

Commit 0dfbb77

Browse files
Vladyslav YakovlevVladyslav Yakovlev
authored andcommitted
Have not resolved problems with Downloads yet
1 parent bb0eb35 commit 0dfbb77

8 files changed

Lines changed: 157 additions & 90 deletions

File tree

MusicPlayer/AppDelegate.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2020
let config = Realm.Configuration(
2121
// Set the new schema version. This must be greater than the previously used
2222
// version (if you've never set a schema version before, the version is 0).
23-
schemaVersion: 1,
23+
schemaVersion: 2,
2424

2525
// Set the block which will be called automatically when opening a Realm with
2626
// a schema version lower than the one set above
2727
migrationBlock: { migration, oldSchemaVersion in
2828
// We haven’t migrated anything yet, so oldSchemaVersion == 0
29-
if (oldSchemaVersion < 1) {
29+
if (oldSchemaVersion < 2) {
3030
// Nothing to do!
3131
// Realm will automatically detect new properties and removed properties
3232
// And will update the schema on disk automatically
@@ -36,6 +36,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
3636
// Tell Realm to use this new configuration object for the default Realm
3737
Realm.Configuration.defaultConfiguration = config
3838

39+
// let realm = try! Realm()
40+
// try! realm.write {
41+
// realm.deleteAll()
42+
// }
43+
3944
window = UIWindow(frame: UIScreen.main.bounds)
4045
window?.rootViewController = BrowserVC()
4146
window?.makeKeyAndVisible()

MusicPlayer/Controllers/BrowserVC.swift

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final class BrowserVC: UIViewController {
2525

2626
private let downloadService = DownloadService.shared
2727

28-
private var downloadsManager: DownloadsManager! //= DownloadsManager()
28+
private let downloadsManager = DownloadsManager()
2929

3030
private let musicFormats = ["mp3", "aac", "aiff", "wav", "alac"]
3131

@@ -78,7 +78,7 @@ final class BrowserVC: UIViewController {
7878
//let queue = DispatchQueue(label: "realm")
7979

8080
//queue.async {
81-
self.downloadsManager = DownloadsManager()
81+
//self.downloadsManager = DownloadsManager()
8282
//self.downloadsManager.queue = queue
8383
//}
8484

@@ -137,7 +137,7 @@ final class BrowserVC: UIViewController {
137137
alertVC.showClearButton = true
138138
alertVC.textFieldPlaceholder = "Name"
139139
alertVC.textFieldText = url.lastPathComponent
140-
alertVC.font = UIFont(name: Fonts.general, size: 21)! // UIFont(name: Fonts.gotham, size: 19)!
140+
alertVC.font = UIFont(name: Fonts.general, size: 21)!
141141
let cancelAction = Action(title: "Cancel", type: .cancel)
142142
let saveAction = Action(title: "Save", type: .normal) { _ in
143143
let songName = alertVC.textFieldText!
@@ -149,12 +149,11 @@ final class BrowserVC: UIViewController {
149149
}
150150

151151
private func downloadSong(with url: URL, with name: String) {
152-
let songDownload = SongDownload()
153-
songDownload.title = name
154-
songDownload.url = url
155-
downloadsManager.addDownload(songDownload)
156-
downloadService.startDownload(with: url, title: name)
157-
print("startDownloadWithUrl *** \(songDownload.url) ***")
152+
let download = SongDownload(url: url, title: name)
153+
downloadsManager.addDownload(download) {
154+
self.downloadService.startDownload(with: url, title: name)
155+
}
156+
print("startDownloadWithUrl *** \(download.url) ***")
158157
}
159158

160159
deinit {
@@ -171,11 +170,11 @@ extension BrowserVC: DownloadServiceDelegate {
171170
}
172171

173172
func downloadServiceFinishedDownloading(to location: URL, with url: URL, title: String) {
173+
downloadsManager.setupStatus(.downloaded, forDownloadWith: url)
174174
let song = Song(url: location)
175175
song.title = title
176176
Library.main.addSong(song)
177177
print("delegate: downloadManagerFinishedDownloading")
178-
downloadsManager.setupStatus(.downloaded, forDownloadWith: url)
179178
}
180179

181180
func downloadServiceDownloadedData(from url: URL, with byteCount: Int64, of totalByteCount: Int64) {

MusicPlayer/Controllers/DownloadsVC.swift

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ final class DownloadsVC: UIViewController {
1212

1313
var downloadsManager: DownloadsManager!
1414

15+
var browserVC: BrowserVC!
16+
1517
private let downloadService = DownloadService.shared
1618

1719
private let titleLabel: UILabel = {
@@ -68,7 +70,9 @@ final class DownloadsVC: UIViewController {
6870
downloadsView.dataSource = self
6971
downloadsView.register(SongDownloadCell.self, forCellWithReuseIdentifier: SongDownloadCell.reuseId)
7072

73+
//downloadService.delegate.remove(browserVC)
7174
downloadService.delegate.add(self)
75+
//downloadService.delegate.add(browserVC)
7276

7377
closeButton.addTarget(self, action: #selector(tapCloseButton), for: .touchUpInside)
7478
clearButton.addTarget(self, action: #selector(tapClearButton), for: .touchUpInside)
@@ -108,7 +112,15 @@ extension DownloadsVC: SongDownloadCellDelegate {
108112
func tapRemoveButton(_ cell: SongDownloadCell) {
109113
guard let indexPath = downloadsView.indexPath(for: cell) else { return }
110114
downloadsManager.download(for: indexPath.item) { download in
111-
self.downloadService.cancelDownload(with: download.url)
115+
if download.status == .downloaded {
116+
self.downloadsManager.removeDownload(download) {
117+
DispatchQueue.main.async {
118+
self.downloadsView.deleteItems(at: [indexPath])
119+
}
120+
}
121+
} else {
122+
self.downloadService.cancelDownload(with: download.url)
123+
}
112124
}
113125
}
114126

@@ -126,8 +138,11 @@ extension DownloadsVC: DownloadServiceDelegate {
126138
func downloadServiceFinishedDownloading(to location: URL, with title: String, url: URL) {
127139
//guard let download = download else { return }
128140
//self.downloadsManager.setupStatus(.downloaded, forDownloadWith: url)
141+
print("*******WILL reloadItems")
129142
self.downloadsManager.indexForDownload(with: url) { index in
143+
print("WILL reloadItems")
130144
if let index = index {
145+
print("WILL reloadItems")
131146
self.downloadsView.reloadItems(at: [IndexPath(item: index, section: 0)])
132147
}
133148
}
@@ -143,6 +158,7 @@ extension DownloadsVC: DownloadServiceDelegate {
143158
}
144159

145160
func downloadServiceDownloadedData(from url: URL, with byteCount: Int64, of totalByteCount: Int64) {
161+
print("########downloadServiceDownloadedData")
146162
downloadsManager.download(with: url) { download in
147163
guard let download = download else { return }
148164
self.downloadsManager.indexForDownload(with: url) { index in
@@ -159,10 +175,13 @@ extension DownloadsVC: DownloadServiceDelegate {
159175
func downloadServiceCanceledDownloading(with url: URL) {
160176
downloadsManager.download(with: url) { download in
161177
guard let download = download else { return }
162-
self.downloadsManager.removeDownload(download)
163178
self.downloadsManager.indexForDownload(with: url) { index in
164179
if let index = index {
165-
self.downloadsView.deleteItems(at: [IndexPath(item: index, section: 0)])
180+
self.downloadsManager.removeDownload(download) {
181+
DispatchQueue.main.async {
182+
self.downloadsView.deleteItems(at: [IndexPath(item: index, section: 0)])
183+
}
184+
}
166185
}
167186
}
168187
}

MusicPlayer/Helpers/MulticastDelegate.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ class MulticastDelegate<T> {
2929
}
3030

3131
func invoke(_ invocation: (T) -> ()) {
32+
print("MulticastDelegates - \(delegates.count)")
3233
for delegate in delegates.allObjects {
34+
print("!!!!!!!invoke delegate:", delegate)
3335
invocation(delegate as! T)
3436
}
3537
}

MusicPlayer/Models/DownloadsManager.swift

Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,59 +10,48 @@ import RealmSwift
1010

1111
class 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

MusicPlayer/Models/Library.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ class Library {
2525
}
2626

2727
func addSong(_ song: Song) {
28-
try! realm.write {
29-
realm.add(song)
28+
DispatchQueue.global(qos: .userInteractive).async {
29+
let realm = try! Realm()
30+
try! realm.write {
31+
realm.add(song)
32+
}
33+
realm.refresh()
3034
}
3135
}
3236

0 commit comments

Comments
 (0)