开发者

Core data storing link to parent record only on last child record stored

开发者 https://www.devze.com 2023-03-11 23:38 出处:网络
I\'ve got a core data model created using XCode 4 that is doing something weird.I have an entity called ProbeObj that has a defined relationship with a second entity called SmokeObj.In the diagram, I\

I've got a core data model created using XCode 4 that is doing something weird. I have an entity called ProbeObj that has a defined relationship with a second entity called SmokeObj. In the diagram, I've created the relationship on ProbObj as ProbeToSmoke and on SmokeObj, I have created the relationship as SmokeToProbe and set it as an inverse relationship to the one defined on ProbeObj. I generated the .h & .m files for these two objects and I include them in the class I created to handle the inserting of the data. Here they are:

//
//  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

My code gathers data from a website, parses it and then adds records to the ProbObj table. The data for the SmokeObj is passed into this part of the program and stored as self.SmokeForThisRun. Here's the code that handles this with irrelevant sections removed out:

- (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);
                    }
                }
            }
        }
    }
}

This code gets called every 1 minute to get the latest information from the web site.

The problem I'm having is that when I look at the data either via code or a SQLLite database viewer app, the only record that has the SmokeObj set is the last record. For example, if SmokeObj = 1 and I collect ProbeObj data 3 times, only the last ProbeObj will have SmokeObj = 1开发者_JAVA百科. The other two will have a blank value for SmokeObj. If I only collect 1 ProbeObj, then it will have the correct SmokeObj value. The problems happens whether I collect data 1 time or 100 times. Only the last record collected has the correct SmokeObj.

Any help will be greatly appreciated.

-NCGrimbo


Your SmokeObj.SmokeToProbe relationship is set one-to-one. This means that any single instance of SmokeObj can have a relationship with only one other single instance of ProbeObj.

So, as you go through the loop, you first assign the SmokeObj in self.SmokeForThisRun to the first created ProbObj referenced by newProb. Then the next time through the loop you change the relationship to the second instance now held by newProb and so on. Only the last created instance of referenced by newProb actually ends up with the value for the relationship set.

The solution is to change the SmokeObj.SmokeToProbe to a to-many relationship so that the same SmokeObj obj can relate to several 'ProbeObj`.

(BTW, you should follow the naming conventions for entity/classes and properties. Entities and class names have the first letter capitialized but properties do not. E.g. SmokeObj.SmokeToProbe should be SmokeObj.smokeToProbe. It makes your code easier for others to read. )

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号