AMapMethodCallDispatcher.m 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //
  2. // AMapMethodCallDispatcher.m
  3. // amap_flutter_map
  4. //
  5. // Created by lly on 2020/11/16.
  6. //
  7. #import "AMapMethodCallDispatcher.h"
  8. @interface AMapMethodCallDispatcher ()
  9. @property (nonatomic, strong) NSRecursiveLock *dictLock;
  10. @property (nonatomic, strong) NSMutableDictionary *callDict;
  11. @end
  12. @implementation AMapMethodCallDispatcher
  13. - (instancetype)init {
  14. self = [super init];
  15. if (self) {
  16. self.dictLock = [[NSRecursiveLock alloc] init];
  17. self.callDict = [[NSMutableDictionary alloc] init];
  18. }
  19. return self;
  20. }
  21. - (void)onMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
  22. [self.dictLock lock];
  23. FlutterMethodCallHandler handle = [self.callDict objectForKey:call.method];
  24. [self.dictLock unlock];
  25. if (handle) {
  26. handle(call,result);
  27. } else {
  28. NSLog(@"call method:%@ handler is null",call.method);
  29. result(nil);
  30. }
  31. }
  32. - (void)addMethodName:(NSString *)methodName withHandler:(FlutterMethodCallHandler)handler {
  33. NSAssert((methodName.length > 0 && handler != nil), @"添加methodCall回调处理参数异常");
  34. [self.dictLock lock];
  35. [self.callDict setObject:handler forKey:methodName];
  36. [self.dictLock unlock];
  37. }
  38. - (void)removeHandlerWithMethodName:(NSString *)methodName {
  39. NSAssert(methodName.length > 0, @"移除methodCall时,参数异常");
  40. [self.dictLock lock];
  41. [self.callDict removeObjectForKey:methodName];
  42. [self.dictLock unlock];
  43. }
  44. - (void)clearAllHandler {
  45. [self.dictLock lock];
  46. [self.callDict removeAllObjects];
  47. [self.dictLock unlock];
  48. }
  49. @end