我是新手dispatch_queue_t
,我的应用程序有两个问题,我认为可以使用 GCD 解决。第一个是在主视图(ViewController)中我有一个由另一个类(AudioViewController)实现的标签,当我在ViewController中进行任何用户交互时,标签停止实现,所以我想如果我使用dispatch_queue_t
这个问题会得到解决。
第二件事是,在同一个主视图(ViewController)中,当我按下贡献时,我调用了另一个类(ContributionViewController),而这个类只访问另一个类(AudioViewController)的实例变量,该变量一直在刷新。当我开始贡献时,我创建了一个循环来获得多个值来用它们进行一些微积分,但这些值都是相同的。
我会在这里放一些代码来清除这些东西。
ViewController.m
- (IBAction)makeContribution:(id)sender
{
NSLog(@"A: Contribution button clicked");
NSLog(@"-= START CONTRIBUTION =-");
cvc = [[ContributionViewController alloc] init];
cvc.avc = self.avc;
// Get NUM_CONTRIBUTIONS contributions to make average.
int contContribution;
for (contContribution = 0; contContribution < NUM_CONTRIBUTIONS; contContribution++) {
[cvc getEachContribution];
}
// Make average
[cvc makeAverage:NUM_CONTRIBUTIONS];
[cvc release];
}
AudioViewController.m
- (void)audioInitializationWithTimeInterval:(float)time
{
NSDictionary* recorderSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:kAudioFormatLinearPCM],AVFormatIDKey,
[NSNumber numberWithInt:44100],AVSampleRateKey,
[NSNumber numberWithInt:1],AVNumberOfChannelsKey,
[NSNumber numberWithInt:16],AVLinearPCMBitDepthKey,
[NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,
[NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
nil];
NSError* error;
NSURL *url = [NSURL fileURLWithPath:@"/dev/null"];
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:recorderSettings error:&error];
//enable measuring
//tell the recorder to start recording:
[recorder record];
if (recorder) {
[recorder prepareToRecord];
recorder.meteringEnabled = YES;
[recorder record];
levelTimer = [NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(levelTimerCallback:) userInfo:nil repeats:YES];
}
else
NSLog(@"%@",[error description]);
}
- (void)levelTimerCallback:(NSTimer *)timer
{
//NSLog(@"-= AVC =-");
[recorder updateMeters];
db = [recorder averagePowerForChannel:0] - DBOFFSET;
db += 120;
db = db < 0 ? 0 : db;
vc.lab_decibel.text = [NSString stringWithFormat:@"%0.0f", db];
}
ContributionViewController.m
- (void)getEachContribution
{
actualContribution = self.avc.db;
NSLog(@"Actual contribution: %f", actualContribution);
NSLog(@"Sum before: %0.2f", sumContribution);
sumContribution += actualContribution;
NSLog(@"Sum After: %0.2f", sumContribution);
}
- (void)makeAverage:(int)numOfContributions
{
self.average = self.sumContribution / numOfContributions;
NSLog(@"Average: %0.2f", self.average);
}
所以,主要dispatch_queue_t
是要解决我的问题以及如何做到这一点?我试过dispatch_queue_t
装上 AudioViewController、ContributionViewController 和 ViewController,但是第一个没有更新标签,第二个崩溃了,第三个标签仍然是 0 值。
感谢您提供解决此问题的任何提示。
编辑 01:
分贝标签一直在变化。