Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage quickly after app launch or quickly in a sharing extension. #1

Open
crspybits opened this issue Mar 6, 2021 · 2 comments
Open
Assignees
Labels
question Further information is requested

Comments

@crspybits
Copy link

Thanks for this package! I'm trying to make use of it to detect if there is a network available in a sharing extension-- quickly after it starts. It's consistently just indicating there is no network connection. But, there is one. Is this expected behavior? Any thoughts?

I also see this behavior, but less consistently, when the app first launches-- and I quickly use this to detect network availability. It will at times incorrectly indicate the network is not available.

@crspybits
Copy link
Author

Let me correct that. It's just not giving me an initial value early into the app launch. It's not that it's indicating that the network is not available.

@mylogon341
Copy link

Dear distant-past user @crspybits
I just had this exact same issue. I ended up using withTaskCancellationHandler to resolve this race condition. Here is a playground file to get you started - although I assume you're probably finished, published and subsetted this project by now

var stashedContinuations = [CheckedContinuation<Bool, Error>]()
var latestResult: ConnectivityResult?

private struct Continuation: Identifiable {
  let id = UUID()
  let continuation: CheckedContinuation<Bool, Error>
}

func hasInternetConnectivity() async throws -> Bool {
  
  if let latestResult = latestResult {
    return latestResult.isConnected
  }
  
  return try await withTaskCancellationHandler(
    handler: {
      cancelStashedContinuations()
    },
    operation: {
      try await withCheckedThrowingContinuation { continuation in
        stashedContinuations.append(.init(continuation: continuation))
      }
    })
}

func continueAllContinuations() {
  
  stashedContinuations.forEach {
    $0.continuation.resume(returning: latestResult?.isConnected == true)
    print(">> responding to a continuation!")
  }
  
  stashedContinuations.removeAll()
  
}

func cancelStashedContinuations() {
  stashedContinuations.forEach({ $0.continuation.resume(returning: false) })
  stashedContinuations.removeAll()
}

Task {
  print("blah")
  
  DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
    continueAllContinuations()
  }
  let conn = try? await hasInternetConnectivity()
  print(conn)
}

That was my draft POC (still need to thoroughly test when implemented). Hopefully its useful for future folk

@rwbutler rwbutler self-assigned this Mar 16, 2022
@rwbutler rwbutler added the question Further information is requested label Mar 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants