我已经到了不幸的地步,我需要重建一个单身人士。
看起来像这样
const localeType = new GraphQLObjectType({
name: 'Locale',
fields: {
resolve: (locale, args) => this[args.locale],
},
});
正如您从中可以看到的,原因是AVPlayer有时会进入失败状态(.status ==。失败),文档特别提到需要重新创建实例本身才能工作任何进一步的。
我用这种单身模式结束了我的应用程序并且有几个月的实时牵引力,所以将此更改为非单一模式可能需要花费太多工作。
所以我的问题是,如何以一种漂亮的方式重新创建这个单身?
答案 0 :(得分:1)
您可以将公共/内部sharedInstance
作为计算属性,从私有_sharedInstance
共享对象。这样,您就无法从类外部分配新实例。还添加一个静态函数,通过为私有_sharedInstance
分配一个新的Myplayer
对象来重新创建实例。
class Myplayer: AVPlayer {
private static var _sharedInstance = Myplayer()
static var sharedInstance: Myplayer {
return Myplayer._sharedInstance
}
static func recreateInstance() {
Myplayer._sharedInstance = Myplayer()
}
}
答案 1 :(得分:1)
每当我需要在(例如)单元测试期间可重置的单例时,我都会使用此模式:
class MyManager {
private static var _sharedInstance: MyManager? = nil
static var sharedInstance: MyManager {
if _sharedInstance == nil {
_sharedInstance = MyManager()
}
return _sharedInstance!
}
static func resetSharedInstance() {
_sharedInstance = nil
}
}
答案 2 :(得分:1)
你走在正确的轨道上。关键是创建一个计算属性,检查status
的{{1}}是否为AVPlayer
,如果是,则重新创建实例。第二个.failed
包含对您的单身人士的引用,并在检查后返回:
private static let
无需重新创建单身人士。
答案 3 :(得分:0)
我使用这个结构来使用单例,所以每次你试图使用对象时它都不会给你零。
public void handleIntent(Intent intent)
{
try
{
if (intent.getExtras() != null)
{
RemoteMessage.Builder builder = new RemoteMessage.Builder("MyFirebaseMessagingService");
for (String key : intent.getExtras().keySet())
{
builder.addData(key, intent.getExtras().get(key).toString());
}
onMessageReceived(builder.build());
}
else
{
super.handleIntent(intent);
}
}
catch (Exception e)
{
super.handleIntent(intent);
}
}