Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

更新单例 service 的实现逻辑 #74

Merged
merged 2 commits into from
May 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions BeeHive/BHServiceManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,26 @@ - (id)createService:(Protocol *)service
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"%@ protocol does not been registed", NSStringFromProtocol(service)] userInfo:nil];
}

id protocolImpl = [[BHContext shareInstance] getServiceInstanceFromServiceName:NSStringFromProtocol(service)];
NSString *serviceStr = NSStringFromProtocol(service);
id protocolImpl = [[BHContext shareInstance] getServiceInstanceFromServiceName:serviceStr];
if (protocolImpl) {
return protocolImpl;
}

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];
if ([[implClass class] respondsToSelector:@selector(singleton)]) {
if ([[implClass class] singleton]) {
if ([[implClass class] respondsToSelector:@selector(shareInstance)])
implInstance = [[implClass class] shareInstance];
else
implInstance = [[implClass alloc] init];

[[BHContext shareInstance] addServiceWithImplInstance:implInstance serviceName:serviceStr];
return implInstance;
}
}

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

return [[implClass alloc] init];
}

#pragma mark - private
Expand Down
2 changes: 1 addition & 1 deletion BeeHive/BHServiceProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@optional

- (BOOL)singleton;
+ (BOOL)singleton;

+ (id)shareInstance;

Expand Down
3 changes: 1 addition & 2 deletions Example/BeeHive/BHUserTrackViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ @interface BHUserTrackViewController()<UserTrackServiceProtocol>

@implementation BHUserTrackViewController


-(BOOL)singleton
+(BOOL)singleton
{
return NO;
}
Expand Down