I can see the exc bad excess on crashylitics , following is the code base . Is there anything wrong I am doing ? Crash is occurring on following line
guard let data = userData, let userDetails = try? JSONDecoder().decode(UserDetailsThreadSafe.self, from: data) else {
class UserDetailsThreadSafe:UserDetails
{
let queue = DispatchQueue(label: "auth-thread-safe-queue", attributes: .concurrent)
static let shared = UserDetailsThreadSafe()
private override init()
{
super.init()
}
required init(from decoder: Decoder) throws {
try super.init(from: decoder)
}
fileprivate func getUserDetails() -> UserDetailsThreadSafe? {
queue.sync {
// perform read and assign value
var userData: Data? = LoginAuth.sharedInstance().defaultStorage?.secureValue(forKey: kUserDetails)
if userData == nil {
userData = UserDefaults.standard.value(forKey: kUserDetails) as? Data
}
guard let data = userData, let userDetails = try? JSONDecoder().decode(UserDetailsThreadSafe.self, from: data) else {
return nil
}
return userDetails
}
}
fileprivate func archieveData() {
queue.async(flags: .barrier) {
// perform writes on data
if let data = try? JSONEncoder().encode(self), data.isEmpty == false, let jsonString = String(data: data, encoding: .utf8) {
LoginAuth.sharedInstance().defaultStorage?.setSecure(jsonString, forKey: kUserDetails)
}
}
}
}
When adding tasks to your queue it is recommended to use "async" with the Barrier flag:
queue.async(flags: .barrier) {...}
Since we use async and not sync, the signature of getUserDetails() should change. This function should now pass back the result values using completion block, and return void.
Related
I am trying to make an API call to the GitLab API to get the projects that are available to a particular user.
I can get one project of an index of my choosing, put it into a ProjectModel with the projectId and the projectName but I can not figure out how to get all of them into an array of ProjectModels.
By printing then I can see them all being printed in the console but it will not let me append them to an array.
It is in the parseJSON function that I am trying to get a hold of all of the projects.
Does anyone have any suggestions?
This is my manager to fetch the projects:
protocol FetchProjectsManagerDelegate {
func didUpdateProjects(_ fetchProjectsManager: FetchProjectsManager, project: ProjectModel?)
func didFailWithError(error: Error)
}
struct FetchProjectsManager {
let projectsURL = "secret"
var delegate: FetchProjectsManagerDelegate?
func fetchProjects(privateToken: String) {
let privateTokenString = "\(projectsURL)projects?private_token=\(privateToken)"
performRequest(with: privateTokenString)
}
func performRequest(with privateTokenString: String) {
// Create url
if let url = URL(string: privateTokenString) {
// Create URLSession
let session = URLSession(configuration: .default)
// Give the session a task
let task = session.dataTask(with: url) { data, response, error in
if error != nil {
self.delegate?.didFailWithError(error: error!)
return
}
if let safeData = data {
if let project = self.parseJSON(safeData) {
self.delegate?.didUpdateProjects(self, project: project)
}
}
}
// Start the task
task.resume()
}
}
func parseJSON(_ projectData: Data) -> ProjectModel? {
let decoder = JSONDecoder()
do {
let decodedData = try decoder.decode([Project].self, from: projectData)
for project in decodedData {
print(project)
}
let projectId = decodedData[0].id
let projectName = decodedData[0].name
let project = ProjectModel(projectId: projectId, projectName: projectName)
return project
} catch {
delegate?.didFailWithError(error: error)
return nil
}
}
}
This is my project model
struct ProjectModel {
let projectId: Int
let projectName: String
}
Your parseJson method only returns a single project instead of all of them, change it to
func parseJSON(_ projectData: Data) -> [ProjectModel]? {
let decoder = JSONDecoder()
do {
let decodedData = try decoder.decode([Project].self, from: projectData)
let projects = decodedData.map { ProjectModel(projectId: $0.id,
projectName: $0.name) }
return projects
} catch {
delegate?.didFailWithError(error: error)
return nil
}
}
and you of course need to update didUpdateProjects so that it takes an array of ProjectModel or call it in a loop
I try use nested DispatchGroup, there is my code :
class TranslateService {
private let myGroup = DispatchGroup()
func translateText(text:[String],closure:#escaping ((_ success:String?,_ error:Error?) -> Void)) {
var translateString: String = ""
var responseError: Error?
for index in 0...text.count - 1 {
let urlString = "https://translate.yandex.net/api/v1.5/tr.json/translate?key=trnsl.1.1.20171105T134956Z.795c7a0141d3061b.dc25bae76fa5740b2cdecb02396644dea58edd24&text=\(text[index])&lang=fa&format=plain&options=1"
if let allowString = Utilities.shareInstance.getQueryAllowedString(url: urlString) {
if let url = URL(string:allowString){
myGroup.enter()
Alamofire.request(url).responseJSON { response in
guard let responseData = response.data else {
self.myGroup.leave()
return
}
do {
let json = try JSONSerialization.jsonObject(with: responseData, options: [])
if let res = json as? [String:Any] {
if let code = res["code"] as? Int {
if code == 200 {
if let textArr = res["text"] as? [AnyObject] {
let flattArr = Utilities.shareInstance.flatStringMapArray(textArr)
if flattArr.count > 0 {
translateString += "،" + flattArr[0]
}
}
}
}
self.myGroup.leave()
}
}catch {
responseError = error
self.myGroup.leave()
}
}
self.myGroup.notify(queue: .main) {
print("Finished all requests.")
print(translateString)
closure(translateString, responseError)
}
}
}
}
}
}
class AddressService {
private let translateService: TranslateService = TranslateService()
private let myGroup = DispatchGroup()
func fetchAddressFromGeographicalLocation(latitude: Double, longitude: Double,closure:#escaping ((_ success:String,_ name:String,_ error:Error?) -> Void)) {
var address: String = ""
let name: String = ""
var responseError: Error?
if let url = URL(string:"https://maps.googleapis.com/maps/api/geocode/json?latlng=\(latitude),\(longitude)&key=AIzaSyAdEzHZfZWyjLMuuW92w5fkR86S3-opIF0&language=fa®ion=IR&locale=fa"){
self.myGroup.enter()
Alamofire.request(url).responseJSON { response in
guard let responseData = response.data else {
self.myGroup.leave()
return
}
do {
let json = try JSONSerialization.jsonObject(with: responseData, options: [])
if let addressDic = json as? [String:Any] {
if let result = addressDic["results"] as? [AnyObject] {
if result.count > 0 {
let flattRes = Utilities.shareInstance.flatMapArray(result)
let item = flattRes[0]
address = item["formatted_address"] as? String ?? ""
var res = address
if res.isContainEnglishCharachter {
self.myGroup.enter()
let resArr = res.components(separatedBy: ",")
var all : [String] = []
for item in resArr {
if item != " " {
all.append(item)
}}
self.translateService.translateText(text: all, closure: {stringAdd,error in
self.myGroup.enter()
if error != nil {
self.myGroup.leave()
}else {
address = stringAdd ?? ""
self.myGroup.leave()
} })
}else {
self.myGroup.leave()
}
}
}
}
}catch {
responseError = error
self.myGroup.leave()
}
self.myGroup.notify(queue: .main) {
// print("Finished all requests.")
closure(address, name, responseError)
}
}
}
}
}
All I want is that the myGroup I put in the class AddressService waiting for the myGroup that I put in the class TranslateService.
but now self.myGroup.notify not call in the AddressService class, So closure not work.
How can solve this problem, Thank you for all the answers.
I think you are over complicating it.
If I understood a bit, what you want to do is the following:
Get an address from the Address service.
Translate some words of that address, one by one, using the translation service.
When using the Address service there is only one call being done, so there is no need to use Dispatch Groups at this point. Dispatch Groups are used to synchronize more than one async call.
For your Translation service you can make good use of the Dispatch groups, since you are doing calls to the service inside a for loop. The problem here is, that the implementation is slightly wrong. You are setting the notification block inside the for loop, and it should be outside, so that it gets only triggered once, when all the calls inside the loop are done.
So move this block outside the for loop in the Translation service:
self.myGroup.notify(queue: .main) {
print("Finished all requests.")
print(translateString)
closure(translateString, responseError)
}
Now "Finished all requests." will only be printed once, when all requests are done.
In the address service you do not need dispatch groups at all. Just wait until the completion block is called.
self.translateService.translateText(text: all, closure: {stringAdd,error in
Everything is done here already.
}
This question already has an answer here:
Return a string from a web scraping function in swift
(1 answer)
Closed 4 years ago.
How can I return a value within an if let statement to be further returned within a function? Here is the code:
func loadUrl(url:String) -> String {
DispatchQueue.global().async {
do {
let appUrl = URL(string:url)!
let data = try Data(contentsOf:appUrl)
let json = try JSONSerialization.jsonObject(with: data) as! [String:Any]
print("Test from do")
if let results = json["results"] as? [[String:Any]] {
print("Test from if let 1")
if let first = results[0] as? [String:Any] {
print("Test from if let 2")
var cityStateLocation = first["formatted_address"]!
return cityStateLocation
//What needs to be returned
}
}
DispatchQueue.main.async {
print("No Error")
}
} catch {
DispatchQueue.main.async {
print("Cannot connect to the server.")
}
}
}
}
What I would like to be able to do is take cityStateLocation and return it in the func, but because it is a part of an if let statement within an .async method I don't know how to do that. Could someone please explain?
EDIT: I need the return value of cityStateLocation to equal a variable in a separate function. Here is the separate function:
#IBAction func continueButton(_ sender: Any) {
var cityState:String
if locationSwitch.isOn == true {
print(location.latitude)
print(location.longitude)
let url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=\(location.latitude),\(location.longitude)&result_type=locality&key=AIzaSyDI-ZacHyPbLchRhkoaUTDokwj--z_a_jk"
loadUrl(url: url)
cityState = loadUrl(url: url)
} else {
cityState = ""
}
CoreDataHandler.saveObject(locationLocality: cityState)
}
Edit 2: The main reason why the "duplicate answer" is not a duplicate is that my code needs to call the return of this function within a separate function then save it to Core Data. Also, my code is not using an array.
You could modify your function to include a closure. For instance:
func loadUrl(url: String, completionHandler: #escaping (_ location: String?) -> (Void)) {
And then, where you want to return it, you'd pass it in as such.
completionHandler(cityStateLocation)
I made it an optional so that, in your fail paths, you could return nil.
Then, where you call the function would change. Using trailing closure syntax, it could look like this:
loadUrl(url: "someurl.com/filepath.txt") { optionalLocation in
guard let nonOptionalLocation = optionalLocation else {
// Location was nil; Handle error case here
return
}
// Do something with your location here, like setting UI or something
}
This is a fairly common pattern when dealing with asynchronous activity, such as working with network calls.
The simplest (perhaps no the prettiest), way of doing this would simply be to declare and instantiate a variable above the dispatch queue. Then you can set the variable equal to whatever you want, within the dispatch queue, and return it afterwards. You can change the type of ret, so that it suits your needs more directly.
func loadUrl(url:String) -> String {
var ret = NSObject()
DispatchQueue.global().async {
do {
let appUrl = URL(string:url)!
let data = try Data(contentsOf:appUrl)
let json = try JSONSerialization.jsonObject(with: data) as! [String:Any]
print("Test from do")
if let results = json["results"] as? [[String:Any]] {
print("Test from if let 1")
if let first = results[0] as? [String:Any] {
print("Test from if let 2")
var cityStateLocation = first["formatted_address"]!
ret = cityStateLocation
//What needs to be returned
}
}
DispatchQueue.main.async {
print("No Error")
}
} catch {
DispatchQueue.main.async {
print("Cannot connect to the server.")
}
}
}
return ret
}
DispatchQueue.global().async will cause the coded included in the closure to be executed at some point the future, meaning you loadUrl function will return (almost) immediately.
What you need is some kind of callback which can be called when you have a result (AKA closure)
This is just another way to approach the problem, the difference between this and Josh's example is simply, I provide an additional closure to handle the errors
func loadUrl(url:String, complition: #escaping (String?) -> Void, fail: #escaping (Error) -> Void) {
DispatchQueue.global().async {
do {
let appUrl = URL(string:url)!
let data = try Data(contentsOf:appUrl)
let json = try JSONSerialization.jsonObject(with: data) as! [String:Any]
print("Test from do")
if let results = json["results"] as? [[String:Any]], !results.isEmpty {
print("Test from if let 1")
let first = results[0]
print("Test from if let 2")
if let cityStateLocation = first["formatted_address"] as? String {
complition(cityStateLocation)
} else {
complition(nil)
}
} else {
complition(nil)
}
} catch let error {
fail(error)
}
}
}
Which you might call using something like...
loadUrl(url: "your awesome url", complition: { (value) in
guard let value = value else {
// No value
return
}
// process value
}) { (error) in
// Handle error
}
I'm trying to fetch data from an API but I can't get it right and I don't know the issue here:
struct BTCData : Codable {
let close : Double
let high : Double
let low : Double
private enum CodingKeys : Int, CodingKey {
case close = 3
case high = 4
case low = 5
}
}
func fetchBitcoinData(completion: #escaping (BTCData?, Error?) -> Void) {
let url = URL(string: "https://api.bitfinex.com/v2/candles/trade:30m:tBTCUSD/hist")!
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else { return }
do {
if let bitcoin = try JSONDecoder().decode([BTCData].self, from: data).first {
print(bitcoin)
completion(bitcoin, nil)
}
} catch {
print(error)
}
}
task.resume()
}
I'd like to be able to access close in every dict and iterate like that:
var items : BTCData!
for idx in 0..<15 {
let diff = items[idx + 1].close - items[idx].close
upwardMovements.append(max(diff, 0))
downwardMovements.append(max(-diff, 0))
}
I get nil. I don't understand how to decode this kind of API where I need to iterate something which is not inside another dict.
EDIT: The above was solved and I'm now struggling to use [BTCData] in another function.
I am trying to use it here :
func fetchBitcoinData(completion: #escaping ([BTCData]?, Error?) -> Void) {
let url = URL(string: "https://api.bitfinex.com/v2/candles/trade:30m:tBTCUSD/hist")!
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else {
completion(nil, error ?? FetchError.unknownNetworkError)
return
}
do {
let bitcoin = try JSONDecoder().decode([BTCData].self, from: data); completion(bitcoin, nil)
//let close52 = bitcoin[51].close
//print(bitcoin)
//print(close52)
} catch let parseError {
completion(nil, parseError)
}
}
task.resume()
}
class FindArray {
var items = [BTCData]()
func findArray() {
let close2 = items[1].close
print(close2)
}
}
fetchBitcoinData() { items, error in
guard let items = items,
error == nil else {
print(error ?? "Unknown error")
return
}
let call = FindArray()
call.items = items
call.findArray()
}
EDIT 2: Solved it with [BTCData](). var items : [BTCData] = [] works too
To decode an array of arrays into a struct with Decodable you have to use unkeyedContainer. Since there is no dictionary CodingKeys are useless.
struct BTCData : Decodable {
let timestamp : Int
let open, close, high, low, volume : Double
init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
timestamp = try container.decode(Int.self)
open = try container.decode(Double.self)
close = try container.decode(Double.self)
high = try container.decode(Double.self)
low = try container.decode(Double.self)
volume = try container.decode(Double.self)
}
}
You don't have to change your JSONDecoder() line.
...
if let bitcoin = try JSONDecoder().decode([BTCData].self, from: data).first {
print(bitcoin)
completion(bitcoin, nil)
}
Just by adding two lines it's even possible to decode the timestamp into a Date value
struct BTCData : Decodable {
let timestamp : Date
let open, close, high, low, volume : Double
init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
timestamp = try container.decode(Date.self)
...
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .millisecondsSince1970
if let bitcoin = try decoder.decode([BTCData].self, from: data).first {
print(bitcoin)
completion(bitcoin, nil)
}
To decode the array and get a value at specific index
do {
let bitcoins = try JSONDecoder().decode([BTCData].self, from: data)
let close52 = bitcoins[51].close
print(close52)
...
You need to use JSONSerialization and cast to [[NSNumber]] to get the result needed
UPDATE
Checking this https://docs.bitfinex.com/v2/reference#rest-public-candles I think this is what you are searching for
Try using this
func fetchBitcoinData(completion: #escaping ([BTCData]?, Error?) -> Void) {
let url = URL(string: "https://api.bitfinex.com/v2/candles/trade:30m:tBTCUSD/hist")!
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else { return }
do {
if let array = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [[NSNumber]]{
var arrayOfCoinData : [BTCData] = []
for currentArray in array{
arrayOfCoinData.append(BTCData(close: currentArray[2].doubleValue, high: currentArray[3].doubleValue, low: currentArray[4].doubleValue))
}
debugPrint(arrayOfCoinData)
completion(arrayOfCoinData, nil)
}
} catch {
print(error)
completion(nil, error)
}
}
task.resume()
}
Log Result
[BitcoinApiExample.BTCData(close: 7838.8999999999996,...]
I've created a model named "File", and it looks OK with Realm Browser:
but when I use the model, it will return error:
libc++abi.dylib: terminating with uncaught exception of type realm::IncorrectThreadException: Realm accessed from incorrect thread.
In my code, I create the Realm object every where I needs add/update:
private var allFiles : Results<File>!
private var downloadingFiles : Results<File>! {
return self.allFiles.filter("completed = false")
}
private var downloadedFiles : Results<File>! {
return self.allFiles.filter("completed = true")
}
private var downloading = false
private var request: Alamofire.Request?
func download() {
let fileRealm = try! Realm()
allFiles = fileRealm.objects(File).sorted("updatedAt")
downloadFile()
}
private func downloadFile() {
if !self.downloading, let file = self.downloadingFiles.first where !file.completed {
self.reqForDownload(file)
}
}
private func reqForDownload(file: File) -> Void {
downloading = true
request = Alamofire
.download(.GET, file.url, destination: { (url, response) -> NSURL in
return NSURL(fileURLWithPath: file.filePath)
})
.progress { [unowned self](bytesRead, totalBytesRead, totalBytesExpectedToRead) in
dispatch_async(dispatch_get_main_queue(), {
let variable = Float(totalBytesRead)/Float(totalBytesExpectedToRead)
debugPrint(variable)
})
}
.response { [unowned self](request, response, data, error) in
if let error = error {
dispatch_async(dispatch_get_main_queue(), {
let fileRealm = try! Realm()
try! fileRealm.write({
file.completed = false
})
self.allFiles = fileRealm.objects(File).sorted("updatedAt")
})
if error.code == NSURLErrorCancelled {
debugPrint("Canceled download")
}
} else {
debugPrint("Downloaded file successfully")
dispatch_async(dispatch_get_main_queue(), {
let fileRealm = try! Realm()
try! fileRealm.write({
file.completed = true
})
self.allFiles = fileRealm.objects(File).sorted("updatedAt")
})
}
self.downloading = false
}
}
I'm new for Realm but I know the Realm is not thread safe, so I'm tried to use the object in main thread as my code but the error still appeared. Please someone help me, thank you.
I've update my code as #TimOliver's suggest, but it still response the same error. New code as below:
private var allFiles : Results<File>!
private var downloadingFiles : Results<File>! {
return self.allFiles.filter("completed = false")
}
private var downloadedFiles : Results<File>! {
return self.allFiles.filter("completed = true")
}
private var downloading = false
private var request: Alamofire.Request?
func download() {
let fileRealm = try! Realm()
allFiles = fileRealm.objects(File).sorted("updatedAt")
downloadFile()
}
private func downloadFile() {
if !self.downloading, let file = self.downloadingFiles.first where !file.completed {
self.reqForDownload(file)
}
}
private func reqForDownload(file: File) -> Void {
downloading = true
request = Alamofire
.download(.GET, file.url, destination: { (url, response) -> NSURL in
return NSURL(fileURLWithPath: file.filePath)
})
.progress { [unowned self](bytesRead, totalBytesRead, totalBytesExpectedToRead) in
dispatch_async(dispatch_get_main_queue(), {
let variable = Float(totalBytesRead)/Float(totalBytesExpectedToRead)
debugPrint(variable)
})
}
.response { [unowned self](request, response, data, error) in
if let error = error {
let fileRealm = try! Realm()
try! fileRealm.write({
file.completed = false
})
self.allFiles = fileRealm.objects(File.self).sorted("updatedAt")
if error.code == NSURLErrorCancelled {
debugPrint("Canceled download")
}
} else {
debugPrint("Downloaded file successfully")
let fileRealm = try! Realm()
try! fileRealm.write({
file.completed = true
})
self.allFiles = fileRealm.objects(File.self).sorted("updatedAt")
}
self.downloading = false
}
}
Like I asked in the comments, if you set an exception breakpoint, you can see exactly which line of code is triggering the Realm exception so you can track in which thread the Realm transaction is occurring, as well as which objects are interacting with it.
If I recall correctly, I believe the closure called in the .response portion of that method doesn't get called on the main thread by default, however you're attempting to modify the file object that was definitely queried for on the main thread.
Short of forcing every closure in there to be called on the main thread, it would be more appropriate to have a primary key property in your file object, hold a reference directly to the primary key value, and then directly query for a thread local version of the file object when you need to update it (i.e. using the Realm.object(ofType: primaryKey:) method.
self.allFiles = fileRealm.objects(File.self).sorted("updatedAt") in the .response() closure was excuted sub thread. So you access self.allFiles on main thread later, it would crash.
Results instances are live, auto-updating views into the underlying data, which means results never have to be re-fetched. They always reflect the current state of the Realm on the current thread, including during write transactions on the current thread.
https://realm.io/docs/swift/latest/#auto-updating-results
So you do not need to re-fetch allFiles. A transaction was committed, allFiles are automatically up to date.