Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 21st, 2012  |  syntax: None  |  size: 4.29 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Data going missing when passed between threads using a Singleton
  2. @property (nonatomic, assign)   ProgressUpdate  *progressUpdate;
  3.        
  4. // Ask delegate to update and display Progress text
  5. -(void) updateProgressSummary:(NSString *)summary detail:(NSString *)detail percentComplete:(NSNumber *)complete {
  6. // These report the proper values
  7.     DLog(@"Reporting Summary - %s", [summary UTF8String]);
  8.     DLog(@"Reporting Detail -  %s", [detail UTF8String]);
  9.     DLog(@"Reporting Complete -  %i", [complete intValue]);
  10.  
  11.     if (summary != nil)
  12.         self.progressUpdate.summaryText = summary;
  13.     self.progressUpdate.detailText = detail;
  14.     self.progressUpdate.percentComplete = complete;
  15.  
  16.     ProgressUpdate *progressUpdateForIssue = [[ProgressUpdate alloc] initWithProgressUpdate:progressUpdate];    
  17.     [self.delegate performSelectorOnMainThread:@selector(displayProgress:) withObject:progressUpdateForIssue waitUntilDone:NO];
  18.  
  19.     [progressUpdateForIssue release];
  20.  
  21. }
  22.        
  23. @property (nonatomic, retain) NSString  *summaryText;
  24. @property (nonatomic, retain) NSString  *detailText;
  25. @property (nonatomic, retain) NSNumber  *percentComplete;
  26.        
  27. -(id) initWithProgressUpdate:(ProgressUpdate *)update {
  28.     if ((self = [super init])) {
  29.         summaryText = [update.summaryText copy];
  30.         detailText = [update.detailText copy];
  31.         percentComplete = [[NSNumber alloc] initWithFloat:[update.percentComplete floatValue]];
  32.     }
  33.     // These report nil values
  34.     DLog(@"Reporting in progUpdate summaryText - %s", [summaryText UTF8String]);
  35.     DLog(@"Reporting in progUpdate detailText -  %s", [detailText UTF8String]);
  36.     DLog(@"Reporting in progUpdate percentComplete -  %i", [percentComplete intValue]);
  37. return self;
  38. }
  39.        
  40. #import <Foundation/Foundation.h>
  41.  
  42.  
  43. @interface ProgressUpdate : NSObject {
  44.     NSString    *summaryText;    
  45.     NSString    *detailText;
  46.     NSNumber    *percentComplete;
  47. }
  48. @property (nonatomic, assign) NSString  *summaryText;
  49. @property (nonatomic, assign) NSString  *detailText;
  50. @property (nonatomic, assign) NSNumber  *percentComplete;
  51.  
  52. -(id) initWith:(ProgressUpdate *)update;
  53.  
  54. @end
  55.        
  56. #import "ProgressUpdate.h"
  57.  
  58. @implementation ProgressUpdate
  59.  
  60. @synthesize summaryText, detailText, percentComplete;
  61.  
  62. -(id) initWith:(ProgressUpdate *)update {
  63.     self = [super init];
  64.  
  65.     self.summaryText = update.summaryText;
  66.     self.detailText = update.detailText;
  67.     self.percentComplete = update.percentComplete;
  68.     return self;
  69. }
  70.  
  71. @end
  72.        
  73. static ProgressController *sharedInstance;
  74.  
  75. + (ProgressController *)sharedInstance {
  76.     @synchronized(self) {
  77.         if (!sharedInstance)
  78.             [[ProgressController alloc] init];              
  79.     }
  80.     return sharedInstance;
  81. }
  82.  
  83. +(id)alloc {
  84.     @synchronized(self) {
  85.         NSAssert(sharedInstance == nil, NSLocalizedString(@"Attempted to allocate a second instance of a singleton ProgressController.", @"Attempted to allocate a second instance of a singleton ProgressController."));
  86.         sharedInstance = [super alloc];
  87.     }
  88.     return sharedInstance;
  89. }
  90. -(id) init {
  91.     if (self = [super init]) {
  92.         [self open];
  93.     }
  94.     return self;
  95. }
  96.  
  97. .........
  98.  
  99. // Ask delegate to update and display Progress text
  100. -(void) updateProgressSummary:(NSString *)summary detail:(NSString *)detail percentComplete:(NSNumber *)complete {
  101.  
  102.     if (summary != nil)
  103.         self.progressUpdate.summaryText = summary;
  104.     self.progressUpdate.detailText = detail;
  105.     self.progressUpdate.percentComplete = complete;
  106.     ProgressUpdate *progressUpdateForIssue = [[ProgressUpdate alloc] initWith:progressUpdate];  
  107.     [self.delegate performSelectorOnMainThread:@selector(displayProgress:) withObject:progressUpdateForIssue waitUntilDone:NO];
  108.     [progressUpdateForIssue release];
  109.  
  110. }
  111.        
  112. // Delegate method to display specific text in Progress label
  113. - (void) displayProgress:(ProgressUpdate *)update {
  114.     [progressSummaryLabel setText:update.summaryText];
  115.     [progressDetailLabel setText:update.detailText];
  116.     [progressBar setProgress:[update.percentComplete intValue]];
  117.     [progressView setNeedsDisplay];
  118. }
  119.        
  120. -(id) initWithProgressUpdate:(ProgressUpdate *)update {
  121.     if ((self = [super init])) {
  122.         summaryText = [update.summaryText copy];
  123.         detailText = [update.detailText copy];
  124.         percentComplete = [[NSNumber alloc] initWithFloat:[update.percentComplete floatValue];
  125.     }
  126.     return self;
  127. }