Skip to content

Commit

Permalink
for performance optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
SoXeon committed Mar 24, 2017
1 parent c869425 commit 38f66e2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 51 deletions.
2 changes: 1 addition & 1 deletion BeeHive.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Pod::Spec.new do |s|

s.license = "GPL"

s.platform = :ios, '7.0'
s.platform = :ios, '8.0'

s.author = { "soxeon" => "[email protected]" }

Expand Down
95 changes: 45 additions & 50 deletions BeeHive/BHServiceManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
#import "BHServiceManager.h"
#import "BHContext.h"
#import "BHAnnotation.h"
#import <objc/runtime.h>

static const NSString *kService = @"service";
static const NSString *kImpl = @"impl";

@interface BHServiceManager()

@property (nonatomic, strong) NSMutableArray *allServices;
@property (nonatomic, strong) NSMutableDictionary *allServicesDict;
@property (nonatomic, strong) NSRecursiveLock *lock;

@end
Expand Down Expand Up @@ -42,7 +44,13 @@ - (void)registerLocalServices
NSArray *serviceList = [[NSArray alloc] initWithContentsOfFile:plistPath];

[self.lock lock];
[self.allServices addObjectsFromArray:serviceList];
for (NSDictionary *dict in serviceList) {
NSString *protocolKey = [dict objectForKey:@"service"];
NSString *protocolImplClass = [dict objectForKey:@"impl"];
if (protocolKey.length > 0 && protocolImplClass.length > 0) {
[self.allServicesDict addEntriesFromDictionary:@{protocolKey:protocolImplClass}];
}
}
[self.lock unlock];
}

Expand All @@ -59,13 +67,15 @@ - (void)registerService:(Protocol *)service implClass:(Class)implClass
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"%@ protocol has been registed", NSStringFromProtocol(service)] userInfo:nil];
}

NSMutableDictionary *serviceInfo = [NSMutableDictionary dictionary];
[serviceInfo setObject:NSStringFromProtocol(service) forKey:kService];
[serviceInfo setObject:NSStringFromClass(implClass) forKey:kImpl];
NSString *key = NSStringFromProtocol(service);
NSString *value = NSStringFromClass(implClass);

[self.lock lock];
[self.allServices addObject:serviceInfo];
[self.lock unlock];
if (key.length > 0 && value.length > 0) {
[self.lock lock];
[self.allServicesDict addEntriesFromDictionary:@{key:value}];
[self.lock unlock];
}

}

- (id)createService:(Protocol *)service
Expand All @@ -76,66 +86,50 @@ - (id)createService:(Protocol *)service
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"%@ protocol does not been registed", NSStringFromProtocol(service)] userInfo:nil];
}

Class implClass = [self serviceImplClass:service];

if ([[implClass class] respondsToSelector:@selector(shareInstance)])
implInstance = [[implClass class] shareInstance];
else
implInstance = [[implClass alloc] init];

if (![implInstance respondsToSelector:@selector(singleton)]) {
return implInstance;
id protocolImpl = [[BHContext shareInstance] getServiceInstanceFromServiceName:NSStringFromProtocol(service)];
if (protocolImpl) {
return protocolImpl;
}

NSString *serviceStr = NSStringFromProtocol(service);

if ([implInstance singleton]) {
id protocol = [[BHContext shareInstance] getServiceInstanceFromServiceName:serviceStr];

if (protocol) {
return protocol;
} else {
[[BHContext shareInstance] addServiceWithImplInstance:implInstance serviceName:serviceStr];
}

} else {
[[BHContext shareInstance] addServiceWithImplInstance:implInstance serviceName:serviceStr];
Class implClass = [self serviceImplClass:service];
Method clzSingleMethod = class_getInstanceMethod(implClass, @selector(singleton));
Method insSingleMethod = class_getClassMethod(implClass, @selector(singleton));
if (clzSingleMethod == NULL &&
insSingleMethod == NULL) {
return [implClass new];
}


implInstance = [[implClass alloc] init];
NSString *serviceStr = NSStringFromProtocol(service);
[[BHContext shareInstance] addServiceWithImplInstance:implInstance serviceName:serviceStr];
return implInstance;
}

#pragma mark - private
- (Class)serviceImplClass:(Protocol *)service
{
for (NSDictionary *serviceInfo in [self servicesArray]) {
NSString *protocolStr = [serviceInfo objectForKey:kService];
if ([protocolStr isEqualToString:NSStringFromProtocol(service)]) {
NSString *classStr = [serviceInfo objectForKey:kImpl];
return NSClassFromString(classStr);
}
NSString *serviceImpl = [[self servicesDict] objectForKey:NSStringFromProtocol(service)];
if (serviceImpl.length > 0) {
return NSClassFromString(serviceImpl);
}

return nil;
}

- (BOOL)checkValidService:(Protocol *)service
{
for (NSDictionary *serviceInfo in [self servicesArray]) {
NSString *protocolStr = [serviceInfo objectForKey:kService];
if ([protocolStr isEqualToString:NSStringFromProtocol(service)]) {
return YES;
}
NSString *serviceImpl = [[self servicesDict] objectForKey:NSStringFromProtocol(service)];
if (serviceImpl.length > 0) {
return YES;
}
return NO;
}

- (NSMutableArray *)allServices
- (NSMutableDictionary *)allServicesDict
{
if (!_allServices) {
_allServices = [NSMutableArray array];
if (!_allServicesDict) {
_allServicesDict = [NSMutableDictionary dictionary];
}
return _allServices;
return _allServicesDict;
}

- (NSRecursiveLock *)lock
Expand All @@ -146,12 +140,13 @@ - (NSRecursiveLock *)lock
return _lock;
}

- (NSArray *)servicesArray
- (NSDictionary *)servicesDict
{
[self.lock lock];
NSArray *array = [self.allServices copy];
NSDictionary *dict = [self.allServicesDict copy];
[self.lock unlock];
return array;
return dict;
}


@end

0 comments on commit 38f66e2

Please sign in to comment.