The applying that I wish to develop is made up of two threads.
1st thread (Scanner)
- I insert the perceived information within the database
- I present the info in a desk
2nd thread (Sender)
- I take the info from the database (max 100) and ship it to our server
- if the info has been accurately despatched I delete it
Every thread should run each n seconds and this worth may be modified from the settings.
For the development of the database I used the next library:
https://github.com/stephencelis/SQLite.swift
For the development of the threads I used:
class RepeatingTimer {
let timeInterval: TimeInterval
init(timeInterval: TimeInterval) {
self.timeInterval = timeInterval
}
non-public lazy var timer: DispatchSourceTimer = {
let t = DispatchSource.makeTimerSource()
t.schedule(deadline: .now() + self.timeInterval, repeating: self.timeInterval)
t.setEventHandler(handler: { [weak self] in
self?.eventHandler?()
})
return t
}()
var eventHandler: (() -> Void)?
non-public enum State {
case suspended
case resumed
//case canceled
}
non-public var state: State = .suspended
deinit {
timer.setEventHandler {}
timer.cancel()
resume()
eventHandler = nil
}
func resume() {
if state == .resumed {
return
}
state = .resumed
timer.resume()
}
func droop() {
if state == .suspended {
return
}
state = .suspended
timer.droop()
}
/*
func finish() {
print("Elimino il Processo")
if state == .canceled {
print("state == .canceled")
return
}
state = .canceled
print("Portato lo stato a canceled")
timer.cancel()
}*/
}
My issues are:
- How one can cease threads to restart them with the brand new worth (n. Seconds)?
- The applying breaks after a while because it alerts to me that the database is blocked due to make use of by one other useful resource
The library for the connection / creation of the Database appears to me very legitimate, I believe my drawback with the implementation of threads.
The applying can even should run on variations previous to iOS 13.
Thanks for collaboration