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

查看:23
本文介绍了使用 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.

显然还有其他方法可以跨视图控制器共享数据,使用单例对象和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?

推荐答案

当数据和对象真正是全局的和/或无法进一步向下推图时.通常不需要这种高级别的存储.同样,您的实现通常应该对应用程序委托几乎一无所知——有什么比单例更糟糕的?God-Singleton :) 如果应用程序委托很复杂,则说明出了点问题.如果应用程序委托的界面对您的许多实现可见(通过 #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 发送到应用程序委托中,所以 可能是存储数据或对象表示的好地方.您还可以考虑将这些消息转发给适当的处理程序.重要的一点——在大多数情况下,您希望初始化沿着对象图向下流动,并从可能的最低点开始(例如,与引用回应用程序委托的许多对象相反).所以你可能会看到应用程序委托设置了一个顶级视图控制器的模型,但是视图控制器可以设置它推送的视图控制器的模型.通过这种方式,您将减少依赖项和控制流,更容易追踪因果关系,并且您将能够更轻松地对其进行测试 - 无需大量全局状态的上下文.

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天全站免登陆