多个 sharedInstance 调用失败
Posted
技术标签:
【中文标题】多个 sharedInstance 调用失败【英文标题】:Multiple sharedInstance called failed 【发布时间】:2013-05-20 09:03:04 【问题描述】:在我的应用程序中,我在多个方法定义中多次调用 sharedinstance , 这是我的代码,
方法一
-(void) showActionSheet:(id)sender forEvent:(UIEvent*)event
if(isQuantity==YES)
[[WebService sharedInstance] getQuantity:^(BOOL result)
if(result)
NSLog(@"success");
NSManagedObjectContext *context = [[DataAccessLayer sharedInstance] managedObjectContext];
Quantity = [context fetchObjectsForEntityName:NSStringFromClass([GetQuantity class]) withSortColumn:nil withSortDescending:TRUE withPredicate:nil];
NSLog(@"array ->%@",Quantity);
isQuantity=NO;
];
popoverController1 = [[TSPopoverController alloc]initWithContentViewController:tableViewController1];
popoverController1.cornerRadius = 5;
popoverController1.titleText = @"Quantity";
popoverController1.popoverBaseColor = [UIColor blackColor];
popoverController1.popoverGradient= NO;
[popoverController1 showPopoverWithTouch:event];
方法二
-(void) showActionSheetw:(id)sender forEvent:(UIEvent*)events
if(isSize==YES)
[[WebService sharedInstance] getDimension:^(BOOL result)
if(result)
NSLog(@"success");
NSManagedObjectContext *context = [[DataAccessLayer sharedInstance] managedObjectContext];
dime = [context fetchObjectsForEntityName:NSStringFromClass([Getdimension class]) withSortColumn:nil withSortDescending:FALSE withPredicate:nil];
NSLog(@"array ->%@",dime);
];
popoverController2 = [[TSPopoverController alloc] initWithContentViewController:tableViewController2];
popoverController2.cornerRadius = 5;
popoverController2.titleText = @"Size";
popoverController2.popoverBaseColor = [UIColor blackColor];
popoverController2.popoverGradient= NO;
// popoverController.arrowPosition = TSPopoverArrowPositionHorizontal;
[popoverController2 showPopoverWithTouch:events];
编辑
- (void) getDimension:(void (^)(BOOL))handler
JBContainedURLConnection *connection = [[JBContainedURLConnection alloc]init ];
[connection initWithGETUrl:IP methodName:GETDIMENSION param:nil andCompletionHandler:^(JBContainedURLConnection *connection, NSError *error, NSString *urlString, NSDictionary *userInfo, NSData *response)
if(error)
NSLog(@"Error: %@", error);
handler(FALSE);
else
if(response == nil)
handler(FALSE);
else
NSManagedObjectContext *context = [[DataAccessLayer sharedInstance] managedObjectContext];
NSArray *existingResults = [context fetchObjectsForEntityName:NSStringFromClass([Getdimension class]) withSortColumn:nil withSortDescending:FALSE withPredicate:nil];
for (NSManagedObject *obj in existingResults)
[context deleteObject:obj];
[[DataAccessLayer sharedInstance] saveContext];
id responseData = [self DictionaryFromResponse:response];
if(responseData == nil)
handler(FALSE);
else
NSLog(@"Dimension Response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
NSArray *data=[responseData objectForKey:@"GetDimensionResult"];
NSLog(@"GetDimensionResult :%@",data);
for( NSDictionary *dict in data)
Getdimension *userDetails = [Getdimension newObject];
[userDetails fillFromDictionary:dict];
[[DataAccessLayer sharedInstance] saveContext];
handler(TRUE);
];
- (void) getQuantity:(void (^)(BOOL))handler
JBContainedURLConnection *connection = [[JBContainedURLConnection alloc]init ];
[connection initWithGETUrl:IP methodName:GETQUANTITY param:nil andCompletionHandler:^(JBContainedURLConnection *connection, NSError *error, NSString *urlString, NSDictionary *userInfo, NSData *response)
if(error)
NSLog(@"Error: %@", error);
handler(FALSE);
else
if(response == nil)
handler(FALSE);
else
NSManagedObjectContext *context = [[DataAccessLayer sharedInstance] managedObjectContext];
NSArray *existingResults = [context fetchObjectsForEntityName:NSStringFromClass([GetQuantity class]) withSortColumn:nil withSortDescending:FALSE withPredicate:nil];
for (NSManagedObject *obj in existingResults)
[context deleteObject:obj];
[[DataAccessLayer sharedInstance] saveContext];
id responseData = [self DictionaryFromResponse:response];
if(responseData == nil)
handler(FALSE);
else
NSLog(@"GetQuantityResult Response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
NSArray *data=[responseData objectForKey:@"GetQuantityResult"];
// NSLog(@"GetDimensionResult :%@",data);
for( NSDictionary *dict in data)
GetQuantity *userDetails = [GetQuantity newObject];
[userDetails fillFromDictionary:dict];
[[DataAccessLayer sharedInstance] saveContext];
handler(TRUE);
];
实例方法
+ (id)sharedInstance
@synchronized(self)
if (manager == nil)
manager = [[self alloc] init];
return manager;
-(id)init
if(self = [super init])
return self;
-(NSString *)NSStringFromDictionaryUsingJSON:(id)dictionary
SBJsonWriter *writer = [[SBJsonWriter alloc]init];
return [writer stringWithObject:dictionary];
-(id)DictionaryFromResponse:(NSData *)response
NSString *responseBody = [[NSString alloc] initWithData:response encoding:NSASCIIStringEncoding];
SBJsonParser *parser = [[SBJsonParser alloc]init];
return [parser objectWithString:responseBody error:nil];
sharedInstance 只能工作一次,即。如果我首先调用任何方法,它的工作,如果调用其他方法第二次应用程序崩溃。任何人都可以帮我解决它
【问题讨论】:
你能发布你的代码吗?你在 webservice 类中写了 sharedInstance 方法 崩溃时的崩溃报告或控制台日志..? @ForamMukundShah 问题已更新,请检查 @vishy 控制台日志没有显示任何内容。完全空白 你的 getDimension 方法没有被调用对吗? 【参考方案1】:我猜 sharedInstance 方法很乱。
应该是
+ (id)sharedInstance
if (manager == nil)
manager = [[self alloc] init];
return manager;
享受编程!
【讨论】:
现在当我第一次调用该方法时,它本身就崩溃了\ 通话记录中没有任何内容。这就是问题 在+ (id)sharedInstance
方法派生类中
我已经提到了有问题的实例方法,只有我声明了 + (id)sharedInstance
我看不到静态 WebService __strong *manager = nil;在共享实例方法中【参考方案2】:
您是否将您的类的实例声明为静态的, 将您的类对象声明为:
静态类名 *manager;
并且在您的 sharedInstance 方法中分配相同的对象。 原因可能是内存对象被释放了,但内存中仍然存在引用。所以当你执行共享实例方法时,它发现了一个对!nil对象的引用并且导致您的应用程序崩溃。
这是 ios 的单例类特性(目标 C)
【讨论】:
我已经在 webservice 类中声明了static WebService __strong *manager = nil;
.. 我应该添加我调用的方法类
不,你不应该在调用类中添加这个。
@peter 您在控制台中遇到的具体错误是什么。如果您将“错误日志”发送给我,这将非常有帮助以上是关于多个 sharedInstance 调用失败的主要内容,如果未能解决你的问题,请参考以下文章
Django queryset 'in' 运算符在第一次调用时失败