编辑:
谢谢@BlackFrog。我想我现在越来越近了,但值(value)观仍然无法实现...
值的设置如[progressController updateProgressSummary:...]中的日志所示,但当我将它们记录在progressUpdate initWithProgressUpdate:...中时为零,如下所示。
我对使用哪个属性,为progressUpdate设置一个属性或为progressUpdate的3个组件中的每个设置的属性感到有些困惑。我已经按照建议将3个单独的属性从assign更改为keep,并且还尝试对整个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的委托(delegate)( View Controller )执行performSelectorOnMainThread以显示详细信息。
当我通过单个NSString时,一切正常,但是我需要传递两个字符串和一个数字,由于performSelectorOnMainThread只能传递一个对象,因此我将它们封装在一个自定义对象中-ProgressUpdate。
数据正确到达ProgressController,但在显示在View Controller中时为空。我将NSLogs放置在各个地方时,我知道这一点。
我想知道它是否与:
任何想法都欢迎。为了清楚起见,代码如下。
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];
}
最佳答案
在init方法中,您仅分配ivars,而不将其保留在新对象中。
重做您的初始化方法,如下所示:
-(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;
}
几点要点:
关于iphone - 使用Singleton在线程之间传递时数据丢失,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5713859/