在用户升级应用程序或从头开始安装应用程序后,我只想运行一段代码(用于数据清理)。
将代码段放入java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:211)
at com.myCompany.client.ClientIOFactory$1.<init>(ClientIOFactory.java:17)
at com.myCompany.client.ClientIOFactory.lambda$clientIOFactoryFromSocket$0(ClientIOFactory.java:15)
at com.myCompany.client.queryData(Client.java:83)
at com.myCompany.client.queryData(Client.java:91)
at com.myCompany.queryOptData(InstantAutomaton.java:153)
at com.myCompany.AutoMain.main(InstantAutomaton.java:426)
方法没有帮助,因为-似乎-在升级后打开应用程序后(没有在升级前强行杀死)didFinishLaunchingWithOptions
事件未触发。
我可以将这段代码放入didFinishLaunchingWithOptions
事件处理程序中,并以applicationWillEnterForeground
标志dataCleanupDone
为条件。有更整洁的解决方案吗?
答案 0 :(得分:1)
清理后,在用户首选项中设置一个标志(kCleanUpCompletedKey
)。检查应用程序启动时是否设置了此标志。
示例:
- (void)applicationDidBecomeActive:(UIApplication *)application {
if ([self shouldDoCleanUp]){
// do your clean up
// after successful clean up:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setBool:YES forKey:kCleanUpCompletedKey];
}
}
- (BOOL)shouldDoCleanUp {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if ([prefs objectForKey:kCleanUpCompletedKey]){
return YES;
}
return NO;
}
答案 1 :(得分:0)
您需要优先在新的App版本中添加appversion,然后 如果prefrence值为0,则调用onUpgradingApp方法,然后将其设置为1并运行此代码
integerValue CurrentAppVersion = 2; // suppose
内部Appdelegate:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSInteger oldVersion = [[defaults objectForKey:@"appVersion"] integerValue];
if (oldVersion == 0) // if old app is running
{
oldVersion = 1;
}
if (oldVersion < CurrentAppVersion)
{
[self onUpgradingApp:oldVersion newVersion: CurrentAppVersion];
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInteger:CurrentAppVersion] forKey:@"appVersion"]; //save new version in preference
}
-(void)onUpgradingApp:(NSInteger)oldVersion newVersion:(NSInteger)newVersion
{
NSString *query;
switch (oldVersion)
{
case 1:
{
// clean data here
}
break;
default:
break;
}
}
答案 2 :(得分:0)
SWIFT版本
您可以使用相同的逻辑。
1-在info.plist文件中定义您的应用版本
示例:捆绑版本字符串,简称:0.1.1
然后,每次使用新版本时,都应增加该变量。
2-在您的控制器中创建一个变量app_version并从该info.plist文件中获取正确的版本。
示例:让version = dict [“ CFBundleShortVersionString”]设置为?字符串
3-使用!UserDefaults.standards.bool检查应用版本是否更改,如果为true,则运行代码,然后将其设置为true
示例:
if (!UserDefaults.standard.bool(forKey: appVersion)) {
/* Your code */
UserDefaults.standard.set(true, forKey: appVersion)
UserDefaults.standard.synchronize();
}
希望这会有所帮助。