-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunloop.m
More file actions
101 lines (80 loc) · 2.89 KB
/
Copy pathrunloop.m
File metadata and controls
101 lines (80 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
// runloop.m
// Block
//
// Created by hairong chen on 16/2/25.
// Copyright © 2016年 hairong chen. All rights reserved.
//
#import "runloop.h"
@implementation runloop
//AFNetworking 中RunLoop的创建
- (void)netWorkRequestThreadEntryPoint:(id)__unused object{
@autoreleasepool {
[[NSThread currentThread]setName:@"AFNetworking"];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
[runLoop run];
}
}
- (NSThread *)networkRequestThread{
static NSThread *_networkRequestThread = nil;
static dispatch_once_t oncePreadicate;
dispatch_once(&oncePreadicate, ^{
_networkRequestThread = [[NSThread alloc]initWithTarget:self selector:@selector(netWorkRequestThreadEntryPoint:) object:nil];
[_networkRequestThread start];
});
return _networkRequestThread;
}
//接到crash的singal 后后动重启RunLoop
- (void)restartRuningRunLoop{
CFRunLoopRef runloop = CFRunLoopGetCurrent();
NSArray *allModes = CFBridgingRelease(CFRunLoopCopyAllModes(runloop));
while(1){
for(NSString *mode in allModes){
CFRunLoopRunInMode((CFStringRef)mode,0.001,false);
}
}
}
//TableView 延迟加载图片新思路
#if TARGET_OS_IPHONE
UIImageView *imageView;
- (void)delayLoadingImage{
UIImage *downLoadImage = nil;
[imageView performSelector:@selector(setImage:)
withObject:downLoadImage
afterDelay:0
inModes:@[NSDefaultRunLoopMode]]
}
#elif TARGET_OS_MAC
//...
#endif
- (void)content:(NSString *(^)(NSString * content))block{
}
//异步测试
- (void)runUnitlBlock:(BOOL(^)())block timeout:(NSTimeInterval)timeout{
NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:timeout];
do{
CFTimeInterval quantum = 0.0001;
CFRunLoopRunInMode(kCFRunLoopDefaultMode,quantum,false);
}while ([timeoutDate timeIntervalSinceNow]>0 &&!block()) ;
}
//异步测试升级
- (BOOL)upgradeRunUnitlBlock:(BOOL(^)())block timeout:(NSTimeInterval)timeout{
__block Boolean fulfilled = NO;
void(^beforeWaiting)(CFRunLoopObserverRef observer,CFRunLoopActivity activity)=
^(CFRunLoopObserverRef observer,CFRunLoopActivity activity){
fulfilled = block();
if (fulfilled) {
CFRunLoopStop(CFRunLoopGetCurrent());
}
};
CFRunLoopObserverRef observer = CFRunLoopObserverCreateWithHandler(NULL,\
kCFRunLoopBeforeWaiting, true, 0, beforeWaiting);
CFRunLoopAddObserver(CFRunLoopGetCurrent(), observer, kCFRunLoopDefaultMode);
//run
CFRunLoopRunInMode(kCFRunLoopDefaultMode, timeout, false);
CFRunLoopRemoveObserver(CFRunLoopGetCurrent(), observer, kCFRunLoopDefaultMode);
CFRelease(observer);
return fulfilled;
}
@end