skip to Main Content
guard let url =  URL(string:"https://blaaaajo.de/getHiddenUsers.php") else { return }
    
let postString = "blockedUsers=245,1150"
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = postString.data(using: String.Encoding.utf8);

do {
    let (responseString, _) = try await URLSession.shared.data(for: request)
    
    if let decodedResponse = try? JSONDecoder().decode([HiddenUsersModel].self, from: responseString){
        gettingBlockedUsers = false
        blockedUsers = decodedResponse
    }
    
} catch {
    print("Error: (error)")
}

the HiddenUsersModel:

struct HiddenUsersModel: Codable {
    var userid: Int
    var nickname: String
}

I’m always getting data not valid

The url and the POST key blockedUsers with the value 245,1150 is 100% correct, I’m also using this API for the web and Android app.

The code on server side doesn’t get executed though, not even at the beginning of the PHP script. So no JSON response is generated.

The error I’m getting:

Error: Error Domain=NSURLErrorDomain Code=-999 "Abgebrochen" UserInfo={NSErrorFailingURLStringKey=https://blaaaajo.de/do_getblockedusers.php, NSErrorFailingURLKey=https://blaaaajo.de/do_getblockedusers.php, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <33660134-3AEC-4416-A917-C0FC64934DB5>.<7>"
), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <33660134-3AEC-4416-A917-C0FC64934DB5>.<7>, NSLocalizedDescription=Abgebrochen}

according to the checked answer in How to fix "NSURLErrorDomain error code -999" in iOS it’s another request is made before the previous request is completed but it’s not true, when I print something before do then it gets printed only once

2

Answers


  1. Chosen as BEST ANSWER

    Ok the problem was really due to task being called twice. The reson for that was that I have passed the showBlockedUsers boolean across multiple views. So passing it leads to for what ever reason the view being called twice and with that interrupting the api, even if you have something like

    if(apiCalled){
        return
    }
        
    apiCalled = true
    
    // API request
    

    This will not prevent the error from happening.

    I'd say it's a bug


  2. I don’t have enough rep to comment but I faced the same issue, including the "if (apiCalled)" safeguard. I added a log, and surprisingly the "print" statement did not execute twice.

    That made me think it had something to do with threads. So I ended up wrapping the function using the .data request in a "Task{}" and it started working.

    Before:

    func callAPI() {
        if calling { return }
        calling = true
        
        ...data(for: request)  // HTTP request
    }
        
    ...
        
    ScrollView{}.refreshable { await callAPI() }
    

    After:

    ...
    ScrollView{}.refreshable { Task { await callAPI() } }
    

    Hoping it can fix someone’s issue pragmatically, though can’t comment on correctness.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search