我有一个使用 XCode 4 创建的核心数据模型,它正在做一些奇怪的事情。我有一个名为 ProbeObj 的实体,它与另一个名为 SmokeObj 的实体定义了关系。在图中,我在 ProbObj 上创建了 ProbeToSmoke 关系,在 SmokeObj 上,我创建了 SmokeToProbe 关系,并将其设置为与 ProbeObj 上定义的关系的反向关系。我为这两个对象生成了 .h 和 .m 文件,并将它们包含在我创建的类中以处理数据的插入。他们来了:
//
// SmokeObj.h
// Stoker Monitor
//
// Created by NCGrimbo on 6/3/11.
// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class BlowerObj, CurBlowerObj, CurProbeObj, ProbeObj, StokerObj;
@interface SmokeObj : NSManagedObject {
@private
}
@property (nonatomic, retain) NSDate * dtEnd;
@property (nonatomic, retain) NSDate * dtStart;
@property (nonatomic, retain) StokerObj * SmokeToStoker;
@property (nonatomic, retain) ProbeObj * SmokeToProbe;
@property (nonatomic, retain) CurProbeObj * SmokeToCurProbe;
@property (nonatomic, retain) CurBlowerObj * SmokeToCurBlower;
@property (nonatomic, retain) BlowerObj * SmokeToBlower;
@end
//
// SmokeObj.m
// Stoker Monitor
//
// Created by NCGrimbo on 6/3/11.
// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//
#import "SmokeObj.h"
#import "BlowerObj.h"
#import "CurBlowerObj.h"
#import "CurProbeObj.h"
#import "ProbeObj.h"
@implementation SmokeObj
@dynamic dtEnd;
@dynamic dtStart;
@dynamic SmokeToStoker;
@dynamic SmokeToProbe;
@dynamic SmokeToCurProbe;
@dynamic SmokeToCurBlower;
@dynamic SmokeToBlower;
@end
//
// ProbeObj.h
// Stoker Monitor
//
// Created by NCGrimbo on 6/3/11.
// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class SmokeObj;
@interface ProbeObj : NSManagedObject {
@private
}
@property (nonatomic, retain) NSDate * dtDataRead;
@property (nonatomic, retain) NSNumber * fCurTemp;
@property (nonatomic, retain) NSNumber * fHighTemp;
@property (nonatomic, retain) NSNumber * fLowTemp;
@property (nonatomic, retain) NSNumber * fTargetTemp;
@property (nonatomic, retain) NSNumber * nAlarmType;
@property (nonatomic, retain) NSNumber * nCtrlAlrmType;
@property (nonatomic, retain) NSString * sBlower;
@property (nonatomic, retain) NSString * sID;
@property (nonatomic, retain) NSString * sUserAssignedName;
@property (nonatomic, retain) SmokeObj * ProbeToSmoke;
@end
//
// ProbeObj.m
// Stoker Monitor
//
// Created by NCGrimbo on 6/3/11.
// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//
#import "ProbeObj.h"
@implementation ProbeObj
@dynamic dtDataRead;
@dynamic fCurTemp;
@dynamic fHighTemp;
@dynamic fLowTemp;
@dynamic fTargetTemp;
@dynamic nAlarmType;
@dynamic nCtrlAlrmType;
@dynamic sBlower;
@dynamic sID;
@dynamic sUserAssignedName;
@dynamic ProbeToSmoke;
@end
我的代码从网站收集数据,对其进行解析,然后将记录添加到 ProbObj 表中。SmokeObj 的数据被传递到程序的这一部分并存储为 self.SmokeForThisRun。这是处理此问题的代码,删除了不相关的部分:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSError *error;
NSArray *chunks = [responseString componentsSeparatedByString: @"{"];
[responseString release];
NSArray *details;
NSArray *arrTheData;
Boolean bIsSensor = false;
Boolean bIsBlower = false;
Boolean bStokerFound = false;
NSRange textRange;
for (int i = 0; i < [chunks count]; i++){
if(!bStokerFound){
}
else{
if ((!bIsBlower) && (!bIsSensor)){
}
else{
if (bIsSensor){
//Set the probe data that is fixed
ProbeObj *newProbe = [NSEntityDescription insertNewObjectForEntityForName:@"ProbeObj" inManagedObjectContext:[Common managedObjectContext]];
newProbe.dtDataRead = [NSDate date];
//newProbe.nSmokeID = [NSNumber numberWithInt: 0];
NSLog(@"Setting ProbeToSmoke = %@", self.SmokeForThisRun);
newProbe.ProbeToSmoke = self.SmokeForThisRun;
//Parse the sensor information then save the data {Parse code removed.}
//Save the Probe Record
if (![[Common managedObjectContext] save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
UIAlertView *saveError = [[UIAlertView alloc]initWithTitle:@"Error Saving the Probe Data." message:[error localizedDescription] delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[saveError show];
[saveError release];
}
else{
NSLog(@"Data saved for %@", newProbe);
}
}
}
}
}
}
此代码每 1 分钟调用一次,以从网站获取最新信息。
我遇到的问题是,当我通过代码或 SQLLite 数据库查看器应用程序查看数据时,唯一设置了 SmokeObj 的记录是最后一条记录。例如,如果 SmokeObj = 1 并且我收集 ProbeObj 数据 3 次,则只有最后一个 ProbeObj 的 SmokeObj = 1。其他两个 SmokeObj 的值为空。如果我只收集 1 个 ProbeObj,那么它将具有正确的 SmokeObj 值。无论我收集数据 1 次还是 100 次都会出现问题。只有收集的最后一条记录具有正确的 SmokeObj。
任何帮助将不胜感激。
-NCGrimbo