-2

good morning community,

I have a very good question that I try to implement in a project but I am something new to combine I want to do a function to check some permissions, but I want to return an AnyPublisher with a tuple inside, could someone help me who has already done it or know how to do it?

I put my code below.

func returnedPermisionReminderAuthorizationStatus(reminderPermission:EKAuthorizationStatus,calendarPermission:EKAuthorizationStatus) -> AnyPublisher<(EKAuthorizationStatus,EKAuthorizationStatus),Never>{

    var reminderPermissionToPass:EKAuthorizationStatus =  .notDetermined
    var calendarPermissionToPass:EKAuthorizationStatus =  .notDetermined
    
    switch (reminderPermission){
    case .notDetermined:
      return Just(reminderPermissionToPass).eraseToAnyPublisher()
    case .restricted:
        reminderPermissionToPass = .restricted
        return Just(reminderPermissionToPass).eraseToAnyPublisher()
    case .denied:
        reminderPermissionToPass = .denied
        return Just(reminderPermissionToPass).eraseToAnyPublisher()
    case .authorized:
        reminderPermissionToPass = .authorized
        return Just(reminderPermissionToPass).eraseToAnyPublisher()
    @unknown default:
        reminderPermissionToPass = .notDetermined
        return Just(reminderPermissionToPass).eraseToAnyPublisher()
    }

}

Is it possible to send a tuple in a just?

George
  • 25,988
  • 10
  • 79
  • 133
  • 1
    It is completely fine to pass a tuple with Just. But in your code, you're returning a single EKAuthorizationStatus value, not a tuple (the function expects to return a tuple). Change them to return a tuple such as `Just((.notDetermined, .notDetermined)).eraseToAnyPublisher` or whatever it is supposed to be for the case. – Cenk Bilgen Aug 10 '21 at 17:33
  • Rather than sending a tuple take advantage of the `Error` type. For example you could send `true` on `authorized`, `false` on `notDetermined` and an error on the other cases. – vadian Aug 10 '21 at 17:50
  • Agree with @vaidan, a status and Error is probably more useful. Even though you can send tuple of two status's and Never failure. – Cenk Bilgen Aug 10 '21 at 18:36
  • For a better way to check for authorization at the head of a Combine pipeline see https://stackoverflow.com/a/60418000/341994 – matt Aug 10 '21 at 22:01

2 Answers2

2

You can send a tuple from Just like this:

func returnedPermisionReminderAuthorizationStatus(
    reminderPermission: EKAuthorizationStatus,
    calendarPermission: EKAuthorizationStatus
) -> AnyPublisher<(EKAuthorizationStatus,EKAuthorizationStatus),Never>{
    Just((reminderPermission,calendarPermission))
        .eraseToAnyPublisher()
}
LuLuGaGa
  • 13,089
  • 6
  • 49
  • 57
0

What about a PassthroughSubject ? If yes then try the following code :

let someResponse = PassthroughSubject<(Bool,String), Never>()

and then to receive

    .onReceive(someResponse, perform: { (boolValue,StringValue) in

    })
Osman
  • 1,496
  • 18
  • 22