使用AppDelegate共享数据 [英] Using the AppDelegate to share data

查看:146
本文介绍了使用AppDelegate共享数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现几个来源说明了如何使用AppDelegate在iOS应用程序中的对象之间共享数据。我已经很无痛地实施了,在我的情况下看起来像是一个很好的方法。想想可以使用AppDelegate来做什么,我想知道应该绘制哪一行。

I've found a couple of sources that explain how to use the AppDelegate to share data between objects in an iOS application. I've implemented it quite painlessly and it looks like a good approach in my situation. Thinking about what could be done using the AppDelegate, I am wondering where the line should be drawn.

显然,还有其他的方法来跨视图控制器共享数据,使用 Singleton对象 NSUserDefaults 何时使用AppDelegate共享数据?在我目前的情况下,我使用这种方法来存储用于推送通知的appleDeviceToken。当用户登录或注销应用程序时,我使用该标记。

Obviously there are other ways to share data across view controllers, use Singleton objects, and NSUserDefaults. When is it appropriate to use the AppDelegate to share data? In my current situation, I use this approach to store the appleDeviceToken used for push notifications. I use that token when users login or logout of the app.

在MyAppDelegate.h中,我声明属性:

In MyAppDelegate.h I declare the property:

@property (nonatomic, retain) NSString *appleDeviceToken;

在MyAppDelegate.m中,我合成了appleDeviceToken,然后设置:

In MyAppDelegate.m I synthesize appleDeviceToken and then set it:

@synthesize appleDeviceToken;    

------------------------------------------------------

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
  NSString *devToken = [[[[deviceToken description]
                          stringByReplacingOccurrencesOfString:@"<"withString:@""]
                         stringByReplacingOccurrencesOfString:@">" withString:@""]
                        stringByReplacingOccurrencesOfString: @" " withString: @""];
  appleDeviceToken = devToken;
}

然后,在我的LoginViewController.m中,我检索它并发布到我的服务器:

Then, in my LoginViewController.m I retrieve it and post it to my server:

  NSString *urlForDeviceTokenPost = [NSString stringWithFormat: @"/api/users/%i/appleDeviceToken", userId];

  MyAppDelegate *appDelegate = (MyAppDelegate*) [UIApplication sharedApplication].delegate;
  NSString *appleDeviceTokenStr = appDelegate.appleDeviceToken;

  AppleDeviceToken *appleDeviceToken = [[AppleDeviceToken alloc] init];
  appleDeviceToken.deviceToken = appleDeviceTokenStr;

  [[RKObjectManager sharedManager] postObject:appleDeviceToken delegate:self];






这个功能很棒,但是这是理想的方法我还应该知道什么?


This works great so far, but is this the ideal approach? What else should I know?

推荐答案

当数据和对象是真正的全局性和/或不能被进一步向下推动时。通常不需要在这个高级别的存储。同样,您的实现通常对应用程序代表通常几乎没有知识 - 比单身人士更糟糕?上帝的单身人士:)如果应用程式代表很复杂,那就出错了。如果应用程序委托人的界面是可见的(通过 #import )到您的许多实现或者直接发送,那么有些错误。

When the data and objects are truly global and/or cannot be pushed further down the graph. Storage at this high level is usually not required. As well, your implementations should usually have little to no knowledge about the app delegate -- What's worse than a Singleton? The God-Singleton :) If the app delegate is complex, something's gone wrong. If the app delegate's interface is visible (via #import) to many of your implementations or they message it directly, then something is wrong.

不需要一个(惯用的ObjC)单例 - 有一个应用程序委托的实例。

There is no need for an (idiomatic ObjC) singleton -- there is one instance of the app delegate.

NSUserDefaults用于持久化小值(顾名思义) - 共享的能力是副作用。

NSUserDefaults is for persistence of small values (as the name implies) -- the ability to share is a side effect.

由于在这种情况下,数据已经被UIKit发送到应用程序委托中, em>可能是存储数据的好地方,或者是对象表示。您也可以考虑将这些消息转发到适当的处理程序。重要的一点 - 在大多数情况下,您希望初始化向下流动对象图,并从可能的最低点流出(例如,与指向应用程序委托的许多对象相反)。因此,您可能会看到应用程序委托设置了顶级视图控制器的模型,但视图控制器可以设置其推送的视图控制器的模型。这样您就可以减少依赖关系和控制流程,因果关系将更容易追踪,您将能够更轻松地进行测试,而无需大量全球状态的上下文。

Since the data is already sent into the app delegate by UIKit in this case, that may be a fine place to store the data, or an object representation of. You might also consider forwarding those messages to an appropriate handler. The important point -- In most cases, you would want initialization to flow down the object graph, and to flow from the lowest points possible (e.g. as opposed to many objects referring back to the app delegate). So you might see the app delegate set a top-level view controller's model, but the view controller can then set the models of the view controllers it pushes. This way you will reduce dependencies and control flow, cause and effect will be easier to trace, and you will be able to test it more easily -- free of the context of a massive global state.

这篇关于使用AppDelegate共享数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆