多个sharedInstance调用失败

时间:2013-05-20 09:03:04

标签: iphone ios6 managedobjectcontext

在我的应用程序中,我在多个方法定义中多次调用sharedinstance, 这是我的代码,

方法1

 -(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];
}

方法2

-(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只能运行一次,即。如果我先调用其中任何一种方法,如果调用其他方法第二次应用程序崩溃了。任何人请帮我解决一下

2 个答案:

答案 0 :(得分:0)

我想sharedInstance方法很麻烦。

应该是

+ (id)sharedInstance
{
    if (manager == nil)
        manager = [[self alloc] init];

return manager;
}

享受编程!

答案 1 :(得分:0)

您是否已将类的实例声明为静态, 将类对象声明为:

static ClassName * manager;

并在sharedInstance方法中分配相同的对象。 原因可能是内存对象已被释放,但引用仍然存在于内存中。因此,当您执行共享实例方法时,它找到了对!nil 对象的引用,导致您的应用程序崩溃。

这是iOS的单例类功能(目标C)