Database is locked while updating data in background - objective-c

I'm working on an application where I've to use sqlite and there are lots of data. I want to load data from "residents_status" column of a table named "tblResident". it is taking too much time to load data of that column (residents_status) more than 8 seconds and other data takes just a second. During the period of fetching the data from "residents_status" column, if I press a button its give me 'query error'
in else part but if I click button after 8 seconds than it is working fine.
Here is my code:
if(sqlite3_exec(database, insert_stmt, NULL, NULL, &error)==SQLITE_OK)
{
NSLog(#"succeddd.....");
IsValidate = true;
if([self checkNetConnection])
{
[self UpdateServerWith_LocalDB];
NSString *Key= [DefaultsValues getStringValueFromUserDefaults_ForKey: #"Key"];
if([[DefaultsValues getStringValueFromUserDefaults_ForKey: #"LastAuthenticationStatus"] isEqualToString:#"false"]){
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil message:#"Your Access is Restrcited,Please contact us" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
}
}
else{
NSLog(#"query error");
ErrorMsg = [NSString stringWithFormat:#"%s", error ];
IsValidate = false;
}

Related

iOS 7.x: app would like to access your motion activity does not show up

I have an app that uses Motion activity feature. On the first run there is a pop up asking user permission to use the feature. But after re-installing the app, the pop up does not show up anymore.
Is this an intended behavior or am I missing something?
so it figures you need to instantiate CMMotionActivityManager for this to work.
make sure you don't miss out on the first line
_motionActivityManager =[[CMMotionActivityManager alloc] init];
if([CMMotionActivityManager isActivityAvailable])
NSLog(#"available");
else
NSLog(#"not available");
[_motionActivityManager startActivityUpdatesToQueue:[NSOperationQueue new] withHandler: ^(CMMotionActivity *activity) {
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *theAlert = [[UIAlertView alloc] initWithTitle:#"activity"
message: activity.description
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[theAlert show];
if(activity.stationary)
{
NSLog(#"stationatry");
}
if(activity.walking)
{
NSLog(#"walking");
}
if(activity.automotive)
{
NSLog(#"automotive");
}
if(activity.cycling)
{
NSLog(#"cycling");
}
if(activity.unknown)
{
NSLog(#"unknown");
}
});
}];

Preventing UIView from switching before UIAlertView appears with Parse (Form not Complete, Logout Yes/No, etc)

I am currently coding an app that has a login and sign-up page with Parse as the backend. I am able to get the username and all of that registered, but I need help with the code that notifies the user if one of the fields are blank. I want to display the UIAlertView of the error and prevent the view from switching in this case (it switches and then displays the notification that the fields were left blank). I know it's probably something simple. Just need a little help. Here is the code below.
-(IBAction)signUpUserPressed:(id)sender
{
PFUser *user = [PFUser user];
user.username = self.userRegisterTextField.text;
user.password = self.passwordRegisterTextField.text;
[user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
[self performSegueWithIdentifier:#"FinishSignup" sender:self];
} else {
[[[UIAlertView alloc] initWithTitle:#"Error" message:[error userInfo][#"error"] delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil] show];
}
}];
}
Help would be much appreciated on this. This is also similar to if you are logging out of an app and it asks you if you would like to logout- stopping the view from switching and displaying the notification.
Thanks!
Before calling signUpInBackgroundWithBlock: check if the fields are empty yourself.
PFUser *user = [PFUser user];
user.username = self.userRegisterTextField.text;
user.password = self.passwordRegisterTextField.text;
if (user.username.length > 0 && user.password.length > 0) {
//sign up only if username/password given
[user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
[self performSegueWithIdentifier:#"FinishSignup" sender:self];
} else {
[[[UIAlertView alloc] initWithTitle:#"Error" message:[error userInfo][#"error"] delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil] show];
}
}];
}
}
else {
//show alert that username or password was left blank
}

For Loop Does Not Work

I am trying to make a guessing app for school.
-(IBAction)check:(id)sender
{
if (answerField.text.length > 0)
{
[self number];
}
else
{
UIAlertView *error = [[UIAlertView alloc]
initWithTitle:#"Error"
message:#"Please type in more than one chracter"
delegate:self
cancelButtonTitle:#"Okay!"
otherButtonTitles:nil];
[error show];
}
}
-(void)number
{
for (int i = 0; i < 100; i++)
{
if (answerField.text > strNumber)
{
hlLabel.text = #"Guess Lower";
answerField.text = #"";
}
if (answerField.text < strNumber)
{
hlLabel.text = #"Guess Higher";
answerField.text = #"";
}
}
}
The Problem: The for loop does not work. It only checks once. Also, everytime it checks, it always says "Guess Higher" when in fact, I guess higher than the computer generate number.
Other Information: I have a method where every time I click the keyboard done from the textfield, it will check the number method.
answerField.text is returning an NSString, not a number.
Try answerField.text.intValue.
And strNumber must be an integer for that to work, btw.
It took me some time to understand what you were trying to do. But I suspect that you are just trying to have a user enter some number in a field to guess a number with up to 100 attempts. Something like that.
But a for loop is not how you can make a user retry something. What you need is some state saved in the object (i.e. an ivar) which counts each attempt.
Second problem here is that it seems you are numerically comparing strings which does not make sense here. You want to compare numerical values.
So your program would likely look like:
-(IBAction)check:(id)sender
{
if (answerField.text.length > 0)
{
[self checkFieldValueAgainstCorrectAnswer]; // use better naming
}
else
{
UIAlertView *error = [[UIAlertView alloc]
initWithTitle:#"Error"
message:#"Please type in more than one chracter"
delegate:self
cancelButtonTitle:#"Okay!"
otherButtonTitles:nil];
[error show];
}
}
-(void)checkFieldValueAgainstCorrectAnswer
{
NSUInteger fieldValue = [answerField.text unsignedIntegerValue];
NSUInteger correctValue = [strNumber unsignedIntegerValue];
if (fieldValue > correctValue) {
// guess lower
} else if(fieldValue < correctValue) {
// guess higher
} else {
// you won!
return;
}
answerField.text = #"";
attempts++;
if (attempts > 100) {
// Too many attemps, you lost!
}
}

how to insert a db in sqlite3? shows error

I'm trying to insert my data into db.db created successfully but data not insert into the database.but the data successfully goes to insertsql after that showing error.pls check my code
-(void)insertDB
{
NSString *dname=[[NSString alloc]init];
dname=[delegate.Name objectAtIndex:0];
sqlite3_stmt *statement;
const char *dbpath=[delegate.databasepath UTF8String];
if (sqlite3_open(dbpath, &contactDB)==SQLITE_OK)
{
NSString *insertSQL=[NSString stringWithFormat:#"INSERT INTO STABLE (NAME) VALUES (\"%#\")",dname];
//insert sql is ok,after that they shows error
const char *insert_stmt=[insertSQL UTF8String];
NSLog(#"const char%#",insert_stmt);
sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement,NULL);
if (sqlite3_step(statement)==SQLITE_DONE)
{
NSLog(#"saved");
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"data" message:#"saved" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[self counting];
}
else
{
NSLog(#"Failed to add new favourites");
NSLog(#"Failed to update row %s", sqlite3_errmsg(contactDB));
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}
[self loadmusic];
}
You say that the error message returned is:
Failed to update row no such table: STABLE
And, well, the problem is exactly that: there is no table called STABLE. You've clearly managed to connect to a database but there is no table there with the name that you expect.
The real question is how and when did you create the table? Chances are the database you're connected to is not the one you think is being used.

Checking for multiple variable value in Xcode

I am creating an iPhone app which is taking many values from the user and assigning them to variables.
I want to display an alert message if more than two of the variables' values are equal to zero.
Basically, if the user has two empty fields, it should show an alert stating that there is insufficient data.
Any idea how to do this?
Your question is a bit vague but what about
Find errors
Display an alert
Somewhere alone the lines of this pseudo code:
int errorCount = 0;
if(var1 == 0) {
errorCount++;
}
if(var2 == 0) {
errorCount++;
}
// check all variables...
// Show alert if there are any errors
if(errorCount > 0) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Title"
message:[NSString stringWithFormat:#"You have %d errors", errorCount]
delegate:nil
cancelButtonTitle:#"GoFightWin"
otherButtonTitles: nil, nil];
[alert show];
[alert release];
}