我有sql以及在我的应用程序中使用的核心数据。现在,如果我做一些更改假设,使用sql查询更新sql db我已经进行了那些更新。如果我进行获取请求并打印数组我看到相同的旧值,除非我退出应用程序并重新启动它。我无法理解这种行为。我想知道如果在sql db中进行了更改,那么这些更改是否反映在核心数据表中?如果没有,那么我是否必须将更新的值保存在coredata中并且是否会导致重复或冗余数据?
这是用于更新值的代码:
我有一个包含值的数组:
- (void) updateGeneraldata3 :(NSArray*) marrDataUpdate
{
sqlite3 *database;
sqlite3_stmt *update_statement = nil;
if(sqlite3_open([strDatabasePath UTF8String], &database) == SQLITE_OK)
{
NSString *strMQueryupdate = [NSString stringWithFormat:@"UPDATE zform_general_data1 SET zChk_Mild='%@', zChk_CTV='%@', zChk_DOA='%@', zChk_ALS1='%@' where zautonumber='%@'",[marrDataUpdate objectAtIndex:0],[marrDataUpdate objectAtIndex:1],[marrDataUpdate objectAtIndex:2],[marrDataUpdate objectAtIndex:3],autoNumberTextField.text];
const char *sql = [strMQueryupdate UTF8String];
if (sqlite3_prepare_v2(database, sql, -1, &update_statement, NULL) != SQLITE_OK) {
NSLog(@"update fails");
}
else
{
sqlite3_bind_text(update_statement, 1, [[marrDataUpdate objectAtIndex:0] UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(update_statement, 2, [[marrDataUpdate objectAtIndex:1] UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(update_statement, 3, [[marrDataUpdate objectAtIndex:2] UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(update_statement, 4, [[marrDataUpdate objectAtIndex:3] UTF8String], -1, SQLITE_TRANSIENT);
int success = sqlite3_step(update_statement);
sqlite3_reset(update_statement);
if (success == SQLITE_ERROR){}
else {}
}
sqlite3_finalize(update_statement);
}
sqlite3_close(database);
}
And after update there is a background function which is executed after every 60 seconds of time .Now if i change the value of these fields on the view using the respective text fields, it updates the value in db using the above code which is also being executed after a certain interval of time :
-(void) functServerPostingData
{
@try
{
@autoreleasepool {
generalDataObject = [[GeneralData alloc] init];
generalData1_Array=[[NSArray alloc]init];
[self checkUsedAutoNumber];
NSError *error;
NSManagedObjectContext *mocGeneral1 = [self managedObjectContext];
NSEntityDescription *entityDescriptionGeneral1 = [NSEntityDescription
entityForName:@"Form_General_Data1" inManagedObjectContext:mocGeneral1];
NSFetchRequest *requestGeneral1;
requestGeneral1 = [[NSFetchRequest alloc] init];
[requestGeneral1 setEntity:entityDescriptionGeneral1];
generalData1_Array=[mocGeneral1 executeFetchRequest:requestGeneral1 error:&error];
NSLog(@"generalData1_Array :%@",generalData1_Array);
if(!generalData1_Array)
{
NSLog(@"Error fetching Login: %@", [error localizedDescription]);
abort();
}
for(int i = 0; i< [generalData1_Array count]; i++) {
for (int j=0;j<[[strAutoNumber componentsSeparatedByString:@","] count] ; j++)
{
if([[[generalData1_Array objectAtIndex:i] Autonumber ] isEqualToString: [[strAutoNumber componentsSeparatedByString:@","]objectAtIndex:j]])
{
generalDataObject.AutoNumber=[NSString stringWithFormat:@"%@",[[generalData1_Array objectAtIndex:i] Autonumber ]];
generalDataObject.Chk_Mild =[[generalData1_Array objectAtIndex:i]Chk_Mild];
generalDataObject.Chk_CTV=[[generalData1_Array objectAtIndex:i]Chk_CTV];
generalDataObject.Chk_PAU=[[generalData1_Array objectAtIndex:i]Chk_PAU];
generalDataObject.Transient=[[generalData1_Array objectAtIndex:i]Transient];
generalDataObject.Chk_ALS1= [[generalData1_Array objectAtIndex:i]Chk_ALS1];
[generalDataObject startPostServerData];
}
}
}
if (generalDataObject.checkRequest==TRUE)
{
[appDelegate CheckInternetConnection];
if(appDelegate.internetWorking==-1)
{
}
else {
serverLinkToHit = [NSString stringWithFormat:@"%@/ReceiveData.aspx?m=InsertData&t=GeneralData",[serverLinkSettings getServerLink ]];
[generalDataObject readServerData:serverLinkToHit postServerData:generalDataObject.finalDataString];
}
}
generalDataObject = nil;
requestGeneral1=nil;
requestGeneral2=nil;
}
}
@catch (NSException *exception)
{
[ExceptionLog LogEntry:exception :@"Error while entering data functServerPostingData"];
}
}
我打印的数组总是显示以前的旧值,无论我在文本字段中输入什么并保存在db.However,在同一个地方我打印sql db的值他们更新。我使用sql选择选择值将其放入数组然后发布?
答案 0 :(得分:1)
没有。这不是Core Data的工作方式。数据库的格式没有记录,你不应该玩它。
我认为这里真正的问题是:为什么你认为你需要使用SQL?看起来你只是更新了一些记录。使用“本机”核心数据非常简单。