ios - 具有类变量的 Iphone sdk 共享实例

标签 ios singleton

我有这个问题,我有一个名为“GraphGenerator”的类,其他类可以调用它来传递一个 View ,从而使该 View 充满图形。 这个 GraphGenerator 类是单例的

+ (GraphGenerator *)sharedInstance
    {
        static GraphGenerator *sharedInstance;
        static dispatch_once_t once;
        dispatch_once(&once, ^{
            //allochiamo la sharedInstance
            sharedInstance = [[self alloc] init];
        });
        return sharedInstance;
    }

这个类在制作图表时使用一些综合属性,如 _graphType 和 _graphData。

    -(void)generateGraphInView:(CPTGraphHostingView*)hostingView ofType:(NSUInteger)type withData:(NSArray*)data andStyle:(NSUInteger)style{

        _graphData=[NSMutableArray arrayWithArray:data];
        _graphStyle=style;
        _graphType=type;

问题是,当我在 ViewController 中的这个单例中开始多次调用时,GraphGenerator 开始制作图表,但不是一次制作图表。似乎类(class)一起做了所有的图,改变了综合的属性值并产生了问题.. 例如,我在方法中进行了这两次调用

[[GraphGenerator sharedInstance]generateGraphInView:graphHost01 ofType:DAILY withData:[_dataDictionary valueForKey:@"day share"] andStyle:BARCHART];

[[GraphGenerator sharedInstance]generateGraphInView:graphHost02 ofType:WEEKLY withData:[_dataDictionary valueForKey:@"week share"] andStyle:BARCHART];

_graphType 设置为 DAILY 紧接着它被设置为 WEEKLY,但设置得太快以至于第一次调用仍在生成他的图表,此时,生成为每周而不是每天。

那么我可以实现什么?我想到了 mutex 或类似的东西,但我不知道如何实现它。 谢谢

最佳答案

创建 UIView 的子类来管理图形创建本身将为您提供一个没有 Singleton 的解决方案。

我们之前使用 CorePlot 来实现尖峰,我们让它像这样工作。

tutorial应该给你大部分的结构。

关于ios - 具有类变量的 Iphone sdk 共享实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17925706/

相关文章:

ios - 如何从数组中的字符串数组中获取每个值

php - 为什么每次运行调用 Foo::getInstance() 的脚本时,PHP 都会创建一个新的单例实例?

ios - 在设备上运行为框架编写的 XCTest

ios - Xcode - 如何更改标签栏项目的位置

java - 如何创建派生类的单例?

java - 如何从 Java POJO 调用单例方法?

c# - 单例设计模式是获得数据库连接的首选

c# - 公共(public)引用应该如何在程序集中传递?

ios - 将按钮数组添加到特定 subview

ios - 在 UITextView 上点击时不调用 UICollectionView didSelectItemAtIndexPath