NSTimer 不准时的处理

NSTimer作为iOS开发中的定时器,用处广泛,最近在使用它作为倒计时时,出了点小状况,如果使用如下方法初始化:

1
NSTimer scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:

如果我在拖动UIScrollView并且不放的时候,Timer并不会被触发,1s的间隔,我猜测可能是Timer附着在主线程上,也就是UI线程,如果主线程需要做UI重绘等大量计算或者被阻塞住,那么Timer的计时准确率就会变低,什么时候来都不确定,只有主线程空闲了,才会准确到达(任然有50-100毫秒误差),后来查阅了文档,果然是说Timer附着在NSRunLoop里,所以要保证Timer的触发,就应该换到其他NSRunLoop去,用下面方法就可以解决:

1
2
yourTimer = [NSTimer timerWithTimeInterval:1.0f target:self selector:@selector(updateTime) userInfo:nil repeats:YES];;
[[NSRunLoop currentRunLoop] addTimer:yourTimer forMode:NSRunLoopCommonModes];

具体参数信息就需要自己查文档了。


curl 用法

curl可以作为下载命令使用,特此记录。

下载文件

下面为直接下载文件,保存为原文件名:
curl -O http://mif.polimercolor.ru/mifsoft/MDict.zip

下面为修改文件名:
curl -O MDict_ver.zip http://mif.polimercolor.ru/mifsoft/MDict.zip

断点续传

下面为断点续传的方法, 使用-C选项,利用-C -选项并指出已经部分下载的文件名,curl将自动下载剩余的部分:
curl -C - -o Smultron-2.2.6.dmg http://jaist.dl.sourceforge.net/Smultron-2.2.6.dmg

使用代理服务器

curl -x 10.1.27.101022 ftp://ftp.funet.fi/README

以上为不需要账号密码的代理服务器下载方法,下面为提供账号密码:
curl -U user:password -x 10.1.27.10:1022 ftp://ftp.funet.fi/README

更多功能

使用 man curl,自行查看帮助。

引用

使用cURL下载文件


Raspberry Pi+Yeelink.net 在线监视系统状态


Raspberry Pi

上图为在Yeelink.net生成的曲线图,数据来源于家中的Raspberry Pi一分钟一次的数据上传,分别上传了CPU,内存,剩余空间的数据。

上传脚本如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#! /bin/sh
# File path: ~/Yeelink/rpi_sensors.sh
apikey=<your api key>
url_cpu=http://api.yeelink.net/v1.0/device/<device no.>/sensor/<sensor no.>/datapoints
url_mem=http://api.yeelink.net/v1.0/device/<device no.>/sensor/<sensor no.>/datapoints
url_disk=http://api.yeelink.net/v1.0/device/<device no.>/sensor/<sensor no.>/datapoints
mem_used=$(free -m | grep "Mem" | egrep "[[:digit:]]+" -o | head -n 2 | tail -n 1)
cpu_load=$(iostat -c | tail -n 2 | egrep "[[:digit:].]+" -o | head -n 1)
disk_avaliable=$(df -l | grep "/dev/sda5" | egrep "[[:digit:]]+" -o | tail -n 3 | head -n 1)
disk_ava=`echo "$disk_avaliable/1024"|bc`
echo $disk_ava
curl -d "{\"value\":$mem_used}" -H "U-ApiKey: $apikey" $url_mem
curl -d "{\"value\":$cpu_load}" -H "U-ApiKey: $apikey" $url_cpu
curl -d "{\"value\":$disk_ava}" -H "U-Apikey: $apikey" $url_disk
exit 0

上面的脚本获取了CPU,内存,剩余空间的值,然后通过Yeelink提供的api地址上传上去,但是还需要通过:

1
crontab -e

来定时运行,内容看起来像这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
*/1 * * * * ~/Yeelink/rpi_sensors.sh

上述代码为每隔1分钟运行一次rpi_sensors.sh,这样就能不间断上传数据了,如果crontab没有修改过默认编辑器的话,建议先修改为vim。
tips:对于上传间隔Yeelink有个限制,不能小于10s。

引用

[linux]在Yeelink云端架设笔记本运行监视器