AMapMethodCallDispatcher.m
1.92 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
//
// AMapMethodCallDispatcher.m
// amap_flutter_map
//
// Created by lly on 2020/11/16.
//
#import "AMapMethodCallDispatcher.h"
@interface AMapMethodCallDispatcher ()
// 用于线程安全的递归锁
@property (nonatomic, strong) NSRecursiveLock *dictLock;
// 存储方法名与处理回调的字典
@property (nonatomic, strong) NSMutableDictionary *callDict;
@end
@implementation AMapMethodCallDispatcher
// 初始化方法,创建锁和字典
- (instancetype)init {
self = [super init];
if (self) {
self.dictLock = [[NSRecursiveLock alloc] init];
self.callDict = [[NSMutableDictionary alloc] init];
}
return self;
}
// 处理 Flutter 侧发来的方法调用
- (void)onMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
[self.dictLock lock];
FlutterMethodCallHandler handle = [self.callDict objectForKey:call.method];
[self.dictLock unlock];
if (handle) {
// 如果有对应的处理回调,则执行
handle(call,result);
} else {
// 没有找到对应回调,输出日志并返回 nil
NSLog(@"call method:%@ handler is null",call.method);
result(nil);
}
}
// 添加方法名与回调处理
- (void)addMethodName:(NSString *)methodName withHandler:(FlutterMethodCallHandler)handler {
NSAssert((methodName.length > 0 && handler != nil), @"添加methodCall回调处理参数异常");
[self.dictLock lock];
[self.callDict setObject:handler forKey:methodName];
[self.dictLock unlock];
}
// 移除指定方法名的回调处理
- (void)removeHandlerWithMethodName:(NSString *)methodName {
NSAssert(methodName.length > 0, @"移除methodCall时,参数异常");
[self.dictLock lock];
[self.callDict removeObjectForKey:methodName];
[self.dictLock unlock];
}
// 清空所有回调处理
- (void)clearAllHandler {
[self.dictLock lock];
[self.callDict removeAllObjects];
[self.dictLock unlock];
}
@end