-
Notifications
You must be signed in to change notification settings - Fork 7
/
Singleton.swift
42 lines (33 loc) · 1.41 KB
/
Singleton.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Service {
//to access methods of this Singleton
static let sharedInstance = Service()
//downloads data from given url and returns the url of saved model in file manager
func dowloadData(url: String, completion: @escaping ((URL) ->()) ){
guard let url = URL(string: url) else {
print("Error: invalid url to be downloaded")
return
}
URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in
if error != nil {
print("Error: model downloading failed")
return
}
DispatchQueue.main.async {
guard let mydata = data else {
print("Error: model downloading failed")
return
}
//successfully downloaded, now write it to the file Manager
let fileManager = FileManager.default
let address = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first
let path = address?.appendingPathComponent("mymodel.obj")
print(path!)
try? mydata.write(to: path!)
completion(path!)
}
}).resume()
}
}
//Service.sharedInstance.dowloadData(url: urlString, completion: { (filePath) in
//
//})