如何在不再次运行应用程序的情况下反映数据库中的变化?
我(启动iPhone的新手)开发了一个跟踪数据使用情况(WiFi,蜂窝)的iPhone应用程序。我正在为用户提供一个选择,以添加新计划,这些计划将放入数据库(SQLITE)中的表中。 我正在选择Picker View中显示计划列表,我想在用户输入新计划后立即在Picker View中刷新数据。截至目前,Pickerview在下一个运行中正在更新:( 提前致谢。 代码在这里:
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[actionSheet addSubview:pickerView];
plans= [[NSMutableArray alloc] init];
NSLog(@"Data base is entering");
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"antara" ofType:@"sqlite"];
FMDatabase *db1 = [[FMDatabase alloc] initWithPath:path1];
[db1 open];
FMResultSet *fResult2= [db1 executeQuery:@"SELECT * FROM main"];
NSLog(@"fresult2 ready");
while ( [fResult2 next])
{
planData = [fResult2 stringForColumn:@"planName"];
[plans addObject:planData];
NSLog(@"The data is =%@",planData);
}
[db1 close];
[pickerView selectRow:1 inComponent:0 animated:NO];
//mlabel.text= [plans objectAtIndex:[pickerView selectedRowInComponent:0]];
//[self setCurrentPlan:[plans objectAtIndex:[pickerView selectedRowInComponent:0]]];
//[mlabel setText:[self getCurrentPlan]];
currentPlan= [plans objectAtIndex:[pickerView selectedRowInComponent:0]];
[plan setText:currentPlan];
[pickerView release];
I am(new to iPhone) developing an iPhone App which tracks the data Usage(wifi,cellular). I am giving an option to the user to add new plans which will be put into a table in the database(sqlite).
I am displaying the list of plans in a picker view and I want to refresh the data in the picker view as soon as user enters a new plan. As of now pickerview is getting updated in the next run :(
thanks in advance.
code is here:
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[actionSheet addSubview:pickerView];
plans= [[NSMutableArray alloc] init];
NSLog(@"Data base is entering");
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"antara" ofType:@"sqlite"];
FMDatabase *db1 = [[FMDatabase alloc] initWithPath:path1];
[db1 open];
FMResultSet *fResult2= [db1 executeQuery:@"SELECT * FROM main"];
NSLog(@"fresult2 ready");
while ( [fResult2 next])
{
planData = [fResult2 stringForColumn:@"planName"];
[plans addObject:planData];
NSLog(@"The data is =%@",planData);
}
[db1 close];
[pickerView selectRow:1 inComponent:0 animated:NO];
//mlabel.text= [plans objectAtIndex:[pickerView selectedRowInComponent:0]];
//[self setCurrentPlan:[plans objectAtIndex:[pickerView selectedRowInComponent:0]]];
//[mlabel setText:[self getCurrentPlan]];
currentPlan= [plans objectAtIndex:[pickerView selectedRowInComponent:0]];
[plan setText:currentPlan];
[pickerView release];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您需要在代码中进行一些分离。尝试一下,告诉我它是否有效
1)将数据从DB提取到单独的函数的逻辑,我们称其为LoadPlans。
2)显然,此代码取自您粘贴的功能,因此从该函数中删除这些行。
3)在正常流下:呼叫加载计划在呼叫上述功能之前
4)每当您在数据库中插入的内容时。再次调用这两个功能,
所有这些都假定计划的数据库条目在同一页面和同一线程中发生,以便您可以控制它,可以在其中调用这些功能。如果此假设不正确,请还解释计划如何以及何时将计划添加到数据库中,请在此处粘贴一些有关DB条目的代码。
I think you need some separation in your code. Try this and tell me if it works
1) Take out the logic of pulling data from DB into a separate function, lets call it loadPlans.
2) Obviously this code is taken from the function you had pasted above and hence, remove these lines from that function.
3) Under normal flow: Call loadPlans before the call to the above mentioned function
4) Whenever you have something inserted in the database. Call these two functions again there
This all assumes that the db entry of the plan is happening on the same page and in the same thread so that you have control over it, where you can call these functions. If this assumption is not true then also explain how and when the plans are getting added to db, paste some code about db entry too here.
您所需要做的就是
在加载完成后将触发代码放在某处,以触发重建您所显示的组件。
您还可以使用类似的方法来检查 pickerView 是否可见以及是否确实需要更新屏幕:
all you need to do is to trigger a rebuilding the components you are showing by
put the trigger code somewhere after the loading has finished.
You may also use something like this to check if the pickerView is visible and if you really need to update the screen: