我有一个声明如下的计时器:
var timer: DispatchSourceTimer?
override func viewDidLoad() {
super.viewDidLoad()
...
timer = DispatchSource.makeTimerSource()
timer!.scheduleRepeating(deadline: .now() + .seconds(60), interval: .seconds(3600), leeway: .seconds(1))
timer!.setEventHandler { [weak self] in
self?.saveData()
}
timer!.resume()
print("TIMER HAS STARTED")
}
此代码在我的viewDidLoad
方法中执行,并在我包含的1分钟延迟后第一次正确触发。然后应该每小时开火一次。我在模拟器中运行我的应用程序,使其始终处于关注状态。事实上它仍在运行。
第一次保存数据时我记录了上午11:45的时间。它直到下午1点再次保存,然后在2点08分再次保存。我错过了什么?
编辑:测试这个计时器时,我每分钟都能获得数据,但对于实际的应用程序,我需要每小时进行一次。
答案 0 :(得分:1)
就我而言,当DispatchSourceTimer
在viewDidLoad
内创建实例时,DispatchSourceTimer
不会触发事件,不确定原因,多次尝试。
因此,请确保将property
作为interval
,并且您也可以尝试缩短delay
和DispatchSourceTimer
// MARK: - Properties
var timer: DispatchSourceTimer!
// MARK: - Functions
// MARK: Overrdes
override func viewDidLoad() {
super.viewDidLoad()
timer = DispatchSource.makeTimerSource()
timer.schedule(deadline: .now() + .seconds(1), repeating: .seconds(5), leeway: .seconds(1))
timer.setEventHandler(handler: { [weak self] in
guard let strongSelf = self else { return }
strongSelf.saveData()
})
timer.resume()
}
func saveData() {
print("❇️Invoked...")
}
参数为了测试而安排的时间表,如下:
public class findLeader {
static class ArrayStack{
private int[] stackArr;
private int tos;//top of stack
public ArrayStack(){
stackArr = new int[10];
tos = -1;
}
public ArrayStack(int size){
stackArr = new int[size];
tos = -1;
}
public ArrayStack(int[] arr, int tos){
stackArr = arr;
this.tos = tos;
}
public boolean isEmpty(){
return(tos == -1);
}
public int peek(){
if(isEmpty()){
return -999;
}
return stackArr[tos];
}
public void push(int x){
if(tos == stackArr.length - 1){
return;
}
stackArr[++tos] = x;
}
public int pop(){
if(isEmpty()){
return -999;
}
int popValue = stackArr[tos];
stackArr[tos] = 0;
--tos;
return popValue;
}
public void print(){
if(isEmpty()){
return;
}
for(int i = 0; i <= tos; ++i){
System.out.print(stackArr[i] + " ");
}
System.out.println();
}
}
public static int leader(ArrayStack myStack){
int initSize = myStack.tos + 1; //gets initial size of stack.
int leader; //initialize leader or majority.
while(!myStack.isEmpty()){
leader = myStack.peek();//set first leader variable to the element at the tos.
System.out.println("leader " + leader); //just for debugging
System.out.println("tos " + myStack.tos); //debugging
//System.out.println(isLeader(myStack, initSize, leader)); //debugging
if(isLeader(myStack, initSize, leader)){
return 1;
}
else{
myStack.pop();
}
System.out.println("after function tos " + myStack.tos); //debugging
}
return -1;
}
public static boolean isLeader(ArrayStack myStack, int initSize, int leader){
ArrayStack as = myStack;
int count = 0;
while(!as.isEmpty()){
if(as.peek() == leader){
as.pop();
++count;
}
else{
as.pop();
}
}
//System.out.println(count);
if(count > initSize / 2)
return true;
else{
return false;
}
}
public static void main(String[] args) {
int[] arr = {2, 5, 6, 2, 8, 2, 8, 2, 2};
ArrayStack stack = new ArrayStack();
stack.push(5);
stack.push(2);
stack.push(6);
stack.push(2);
stack.push(8);
stack.push(2);
stack.push(2);
stack.push(2);
stack.push(5);
System.out.println(leader(stack));
}