我遇到两个在同一个View Controller中运行的定时器的问题。我需要其中一个在另一个无效时启动,并在再次点击第一个开始按钮后重新开启。我尝试创建两个变量并成功构建,但行为不稳定。什么是正确的方法?感谢
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXNAMELENTH 64
#define MAXDATALENTH 1465
typedef struct node
{
char name[MAXNAMELENTH];
char data[MAXDATALENTH];
struct node* left;
struct node* right;
}node;
node* root;
node* search(node ** tree, char *key, int count, FILE *fp_out);
node* insertion(node* r, char *key, char *value);
void deltree(node * tree);
char* strtok_r(char *str, const char *delim, char **nextp);
int main(int argc, char *argv[])
{
node *root;
node *tmp;
FILE *fp;
FILE *outputfile;
FILE *keyfile;
FILE *fp_key;
int i;
int counter = 0;
int bufsize = MAXDATALENTH + MAXNAMELENTH;
int keyfilelen = MAXNAMELENTH;
char *keyread;
char *buffer,*saveptr;
char *line = NULL;
char *keyin = NULL;
char *valuein = NULL;
char inputkey[MAXNAMELENTH];
char *target_key = NULL;
char *keyline = NULL;
root = NULL;
/* Inserting nodes into tree */
buffer = (char *)malloc(bufsize * sizeof(char));
if (buffer == NULL)
{
exit(1);
}
fp = fopen(argv[1], "r");
outputfile = fopen("outputfile.txt", "a");
while (1)
{
fgets(line, bufsize, fp);
buffer = line;
keyin = strtok_r(buffer, ",", &saveptr);
valuein = strtok_r(NULL, "\0", &saveptr);
insertion(root, keyin, valuein);
}
node* insertion(node* r, char *key, char *value)
{
if(r==NULL) // BST is not created created
{
r = (struct node*) malloc(sizeof(struct node)); // create a new node
// insert data to new node
r->name = key;
r->data = value;
// make left and right childs empty
r->left = NULL;
r->right = NULL;
}
// if the data is less than node value then we must put this in left sub-tree
else if(strcmp(key, r->name) < 0){
r->left = insertion(r->left, key, value);
}
// else this will be in the right subtree
else if (strcmp(key, r -> name) > 0){
r->right = insertion(r->right, key, value);
}
else {
if(strcmp(value, r -> data) > 0){
r -> left = insertion(r -> left, key, value);
}
else if(strcmp(value, r->data) < 0){
r -> right = insertion(r->right, key, value);
}
}
return r;
}
答案 0 :(得分:0)
所以你需要一种“触发器”定时器效果吗?
您是否考虑过检查失效来替换var timerHasFinishedRunning: Bool = false
?
e.g。
// When you invalidate, rather
timer1.invalidate()
timer1 = nil
// As goes for timer2
timer2.invalidate()
timer2 = nil
这样,你可以让你的计时器检查完成视图计算属性:
var timer1HasFinishedRunning: Bool {
return self.timer1 == nil
}
var timer1HasFinishedRunning: Bool {
return self.timer2 == nil
}
另外你提到他们表现得“不规律”,你能详细说明吗?你的计时器间隔是1秒,所以如果发生任何不稳定的情况,“1秒内”可能是因为间隔时间过长。例如每次检查每次只进行一次,因此有时可能需要1.999999秒才能注意到计时器无效。
就个人而言,我的间隔为0.1而不是1.0,以提高准确度。