lucas34 / SwiftQueue

Job Scheduler for IOS with Concurrent run, failure/retry, persistence, repeat, delay and more
MIT License
410 stars 43 forks source link

Can't persist jobs, Thread 1: "Invalid type in JSON write (__SwiftValue)" #435

Closed davidsoukup closed 1 year ago

davidsoukup commented 1 year ago

Hello, I have I problem with persisetncy. I got error Thread 1: "Invalid type in JSON write (__SwiftValue)", I know i'ts something with saving jobs into UserDefaults, But I don't know how to fix it.

This is job builder: `let queueManager = SwiftQueueManagerBuilder(creator: ToketronJobCreator()).set(persister: UserDefaultsPersister(key: "SyncQueue")).build()

   JobBuilder(type: RefreshTokenJob.type)
       .with(params: ["RefreshTokenJob": MobileApi.RefreshTokenReq(token: deviceToken.base64EncodedString())])
       .persist()
       .internet(atLeast: .cellular)
       .schedule(manager: queueManager)`

And this is job itself: `class RefreshTokenJob: Job {

public static let type = "RefreshToken"

private let mobileApi: MobileApi = MobileApi.shared
let data: [String: Any]

required init?(params: [String: Any]) {
    self.data = params
}

func onRun(callback: SwiftQueue.JobResult) {
    Task {
        print("queueLog: doing RefreshToken job")
        let params: MobileApi.RefreshTokenReq = data["RefreshTokenJob"] as! MobileApi.RefreshTokenReq
        let result = try await mobileApi.postRefreshToken(data: params)
        if result.ok == true {
            print("queueLog: RefreshToken succesfull")
            callback.done(.success)
        } else {
            print("queueLog: RefreshToken fail")
            callback.done(.fail(result.error as! Error))
        }
    }
}

func onRetry(error: Error) -> SwiftQueue.RetryConstraint {
    print("queueLog: onRetry RefreshToken")
    return RetryConstraint.retry(delay: 5)
}

func onRemove(result: SwiftQueue.JobCompletion) {
   switch result {
   case .success:
       break
   case .fail(_):
       break
   }
}

}`

lucas34 commented 1 year ago

Do you have the full stacktrace ? Perhaps you can write your own serialiser and persister. Probably something is not serialisable in the param of your job.

davidsoukup commented 1 year ago

I already fixed it, you are right, some properties were not serializable. I am stupid, sorry for this unnecessary issue report.

lucas34 commented 1 year ago

I think we could also make the library more reliable and catch this kind of issue