2011-04-19 118 views
2

编辑线程间传递时失踪:数据使用辛格尔顿

感谢@BlackFrog。我觉得我现在更近,但数值仍然没有得到打通......

的值设置为[progressController updateProgressSummary:...]中所示的日志,当我在progressUpdate登录他们,但都无initWithProgressUpdate:....如下所示。

我对使用哪一个属性progressUpdate或为progressUpdate的3个组件中的每个组件设置的属性稍有困惑。我已经将3个单独的属性从赋值中更改为保留,并且也尝试对整体progressUpdate属性也进行相同操作(此处未显示)。

progressController.h

......

@property (nonatomic, assign) ProgressUpdate *progressUpdate; 

progressController.m

// Ask delegate to update and display Progress text 
-(void) updateProgressSummary:(NSString *)summary detail:(NSString *)detail percentComplete:(NSNumber *)complete { 
// These report the proper values 
    DLog(@"Reporting Summary - %s", [summary UTF8String]); 
    DLog(@"Reporting Detail - %s", [detail UTF8String]); 
    DLog(@"Reporting Complete - %i", [complete intValue]); 

    if (summary != nil) 
     self.progressUpdate.summaryText = summary; 
    self.progressUpdate.detailText = detail; 
    self.progressUpdate.percentComplete = complete; 

    ProgressUpdate *progressUpdateForIssue = [[ProgressUpdate alloc] initWithProgressUpdate:progressUpdate];  
    [self.delegate performSelectorOnMainThread:@selector(displayProgress:) withObject:progressUpdateForIssue waitUntilDone:NO]; 

    [progressUpdateForIssue release]; 

} 

但随后几毫秒后....对象内部..他们没有。

progressUpdate.h

.....

@property (nonatomic, retain) NSString *summaryText; 
@property (nonatomic, retain) NSString *detailText; 
@property (nonatomic, retain) NSNumber *percentComplete; 

progressUpdate.m

-(id) initWithProgressUpdate:(ProgressUpdate *)update { 
    if ((self = [super init])) { 
     summaryText = [update.summaryText copy]; 
     detailText = [update.detailText copy]; 
     percentComplete = [[NSNumber alloc] initWithFloat:[update.percentComplete floatValue]]; 
    } 
    // These report nil values 
    DLog(@"Reporting in progUpdate summaryText - %s", [summaryText UTF8String]); 
    DLog(@"Reporting in progUpdate detailText - %s", [detailText UTF8String]); 
    DLog(@"Reporting in progUpdate percentComplete - %i", [percentComplete intValue]); 
return self; 
} 

更新结束

我需要一些帮助,在传球数据从一个线程到另一个线程的自定义类。它在通过之前,但在抵达后消失。我尝试了我所知道的一切,但无济于事。

我的后台线程调用ProgressController并传递当前进度的详细信息。然后在ProgressController的委托(视图控制器)上执行SelectorOnMainThread来显示细节。

当我传递单个NSString时,它一切正常,但我需要传递两个字符串和一个数字,而performSelectorOnMainThread只能传递一个对象,我已将这些封装在自定义对象 - ProgressUpdate中。

数据正确通过ProgressController,但在View控制器中显示时为空。我知道这是因为我已经把NSLog放在了不同的地方。

不知它做:

  • 多线程和自定义对象

  • 的事实,ProgressController是单身,这就是为什么我那么每次alloc'd新ProgressUpdate它的名字,但这并没有帮助。

任何想法的欢迎。为了清楚起见,代码如下。

ProgressUpdate.h

#import <Foundation/Foundation.h> 


@interface ProgressUpdate : NSObject { 
    NSString *summaryText;  
    NSString *detailText; 
    NSNumber *percentComplete; 
} 
@property (nonatomic, assign) NSString *summaryText; 
@property (nonatomic, assign) NSString *detailText; 
@property (nonatomic, assign) NSNumber *percentComplete; 

-(id) initWith:(ProgressUpdate *)update; 

@end 

ProgressUpdate.m

#import "ProgressUpdate.h" 

@implementation ProgressUpdate 

@synthesize summaryText, detailText, percentComplete; 

-(id) initWith:(ProgressUpdate *)update { 
    self = [super init]; 

    self.summaryText = update.summaryText; 
    self.detailText = update.detailText; 
    self.percentComplete = update.percentComplete; 
    return self; 
} 

@end 

ProgressController.m

static ProgressController *sharedInstance; 

+ (ProgressController *)sharedInstance { 
    @synchronized(self) { 
     if (!sharedInstance) 
      [[ProgressController alloc] init];    
    } 
    return sharedInstance; 
} 

+(id)alloc { 
    @synchronized(self) { 
     NSAssert(sharedInstance == nil, NSLocalizedString(@"Attempted to allocate a second instance of a singleton ProgressController.", @"Attempted to allocate a second instance of a singleton ProgressController.")); 
     sharedInstance = [super alloc]; 
    } 
    return sharedInstance; 
} 
-(id) init { 
    if (self = [super init]) { 
     [self open]; 
    } 
    return self; 
} 

......... 

// Ask delegate to update and display Progress text 
-(void) updateProgressSummary:(NSString *)summary detail:(NSString *)detail percentComplete:(NSNumber *)complete { 

    if (summary != nil) 
     self.progressUpdate.summaryText = summary; 
    self.progressUpdate.detailText = detail; 
    self.progressUpdate.percentComplete = complete; 
    ProgressUpdate *progressUpdateForIssue = [[ProgressUpdate alloc] initWith:progressUpdate]; 
    [self.delegate performSelectorOnMainThread:@selector(displayProgress:) withObject:progressUpdateForIssue waitUntilDone:NO]; 
    [progressUpdateForIssue release]; 

} 

RootViewController.m

// Delegate method to display specific text in Progress label 
- (void) displayProgress:(ProgressUpdate *)update { 
    [progressSummaryLabel setText:update.summaryText]; 
    [progressDetailLabel setText:update.detailText]; 
    [progressBar setProgress:[update.percentComplete intValue]]; 
    [progressView setNeedsDisplay]; 
} 

回答

2

在init方法中,您只分配ivars并且不将它们保留在新对象中。 重做init方法如下所示:

-(id) initWithProgressUpdate:(ProgressUpdate *)update { 
    if ((self = [super init])) { 
     summaryText = [update.summaryText copy]; 
     detailText = [update.detailText copy]; 
     percentComplete = [[NSNumber alloc] initWithFloat:[update.percentComplete floatValue]; 
    } 
    return self; 
} 

几点:

  • 你不应该在init方法使用存取
  • 重命名你的init方法是很多明确
  • 在@property中,更改分配以保留
0

尝试删除语句'[progressUpdateForIssue release];'在方法

' - (void)updateProgressSummary:(NSString *)摘要细节:(NSString *)细节percentComplete:(NSNumber *)complete'。

还可以在类ProgressUpdate中将属性从'assign'更改为'retain'。 您可以在dealloc方法中释放这些属性。

祝你好运。

+0

感谢Sreehari,但它没有区别。 – Chris 2011-04-19 09:52:18