NSManaged object sometimes is null and causes crash - objective-c

I am using coredata and often my app crashes because one of the properties of a managed object turn out null. For example when i set text of a UILabel to a text property of the managed object it displays NULL
To find the problem I overrode the setter which assigns the NSManagedObject to a instance variable. In the setter I use NSLog to print the object:
NSLog(#"TSC: setting segment: %#", segment);
its output:
2012-11-05 21:01:56.758 JetLagged[4505:907] TSC: setting segment: <Segment: 0x1d0ab790> (entity: Segment; id: 0x1d0a7f60 <x-coredata://BF6F2D98-0658-4005-8BEF-9FE6B398AF86/Segment/p8> ; data: {
arrivalDate = "2012-11-11 05:00:17 +0000";
arrivalLeg = "0x1d0abba0 <x-coredata://BF6F2D98-0658-4005-8BEF-9FE6B398AF86/IteneryLeg/p11>";
departureDate = "2012-11-10 19:00:17 +0000";
departureLeg = "0x1d0abba0 <x-coredata://BF6F2D98-0658-4005-8BEF-9FE6B398AF86/IteneryLeg/p11>";
iteneryLegs = (
"0x1d0abba0 <x-coredata://BF6F2D98-0658-4005-8BEF-9FE6B398AF86/IteneryLeg/p11>"
);
trip = "0x1c58add0 <x-coredata://BF6F2D98-0658-4005-8BEF-9FE6B398AF86/Trip/p5>";
})
All looks fine, however app still displayes null in some places or crashes. I catch the problem I added some NSAsserts and when I ran the code below in the same run as the output above it failed the assertion. Here is the assert code (segmentToCheck is segment from above).
NSSet *legs = [segmentToCheck valueForKey:#"iteneryLegs"];
NSAssert([legs count] != 0, #"Error: nolegs %#", legs);
its output:
2012-11-05 21:01:56.763 JetLagged[4505:907] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error: nolegs (null)'
When i run the app again (without any data modification) it works fine. Any idea what it might be?

Looks like a typo: #"itineryLegs" instead of #"iteneryLegs".

Related

How to check prefix of string inside an NSDictionary?

advertisementData contains:
Advertisement data {
kCBAdvDataIsConnectable = 1;
kCBAdvDataLocalName = "Ali \n";
kCBAdvDataServiceUUIDs = (
"43200-B827-EB74-0E24-192168001107"
);
}
if(advertisementData[#"kCBAdvDataServiceUUIDs"] hasPrefix:#"432"){
NSLog("It contains this string");
}
When i execute the code it gives me error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CBUUID hasPrefix:]: unrecognized selector sent to instance 0x170034260'.
kCBAdvDataServiceUUIDs is a CBUUID. Use uuidString to get the string.

*** glibc detected *** ./test: free(): invalid pointer:

I am trying to fetch and store ID and Status from Database using statements ID = r->getInt(1) and stat = r->getString(10) where I have declared ID as int and stat as string.
Where first field is an ID containing value 1 and 10th field is status containing value 'A'.
If I try to fetch only ID, my code works fine. Else, when I try to fetch status as well, then while running the code, I am getting below mentioned error:
*** glibc detected *** ./test: free(): invalid pointer: 0x000000001c3f5a60 ***
======= Backtrace: =========
/lib64/libc.so.6[0x3d23e71684]
/lib64/libc.so.6(cfree+0x8c)[0x3d23e74ccc]
/usr/lib64/libstdc++.so.6(_ZNSsD1Ev+0x3a)[0x3d28a9dc1a]
./test[0x401837]
./test(__gxx_personality_v0+0x310)[0x401120]
/lib64/libc.so.6(__libc_start_main+0xf4)[0x3d23e1d8b4]
./test(__gxx_personality_v0+0x89)[0x400e99]
Please help.
Adding just the basics of what I am trying to achieve:
char loader_session[10];
char sql_query[500];
int ID;
string STATUS;
string CURRENT_CNT_FILE
env = Environment::createEnvironment (Environment::DEFAULT);
conn = env->createConnection( user, passwd, db);
strcpy(loader_session,"TEST");
sprintf(sql_query,"SELECT ID, SESSION_NAME, SRC_SUCCESS_PATH, SRC_CDR_PATH, LOG_FILE, CURRENT_CNT_FILE, LOG_MAX_FILE_CNT, LOG_MAX_FILE_SIZE, PROCEDURE_NAME, STATUS, ERROR_CDR_PATH, LOCK_FILE_PATH FROM TEST_DB WHERE SESSION_NAME = '%s'",loader_session);
Statement* const s = conn->createStatement(sql_query);
s->setPrefetchRowCount(10);
ResultSet* const r = s->executeQuery();
while (r->next())
{
ID = r->getInt(1);
STATUS = r->getString(10);
CURRENT_CNT_FILE = r->getString(6);
}
Ok I got a temporary solution for my question. Just by setting MALLOC_CHECK_ to 0, I am not getting the error and my code is running fine. Don't know the side effects of this, but as of now, it's working fine.

Get dropped mail message from Apple Mail in Cocoa

I googled and read docs. Searched on SO, found answers that propose usage of Applescript, but I can't use Applescript because of sandbox.
I am working on Cocoa application (written in Objective-C and sandboxed). I am implementing drag-and-drop from Apple Mail application. App should receive or copy eml mail message as Finder does.
I tried looking at NSPromisedFilesPboardType but hardly could figure out how I should work with it. I tried such code in - (BOOL)acceptDrop:(id<NSDraggingInfo>)info method:
if ([info.draggingPasteboard.types containsObject:#"MV Super-secret message transfer pasteboard type"]) {
NSURL *tempFolder = [FM createTempFolderInFolder:AppDataDir()];
NSArray *filenames = [info namesOfPromisedFilesDroppedAtDestination:tempFolder];
// tempFolder is filepath of generated temp folder in appdata directory so it is accessible for sandboxed app
// filenames becomes nil
return YES;
}
When I try iterating through all types in pasteboard, that what I see:
Dropped types are:
Type: dyn.ah62d4rv4gu8y4zvanr41a3pwfz30n25wqz4ca5pfsr30c35feb4he2pssrxgn6vasbu1g7dfqm10c6xeeb4hw6df (null)
Type: MV Super-secret message transfer pasteboard type (null)
Type: dyn.ah62d4rv4gu8zg7puqz3c465fqr3gn7bakf41k55rqf4g86vasbu1g7dfqm10c6xeeb4hw6df (
{
account = Gmail;
id = 6622;
mailbox = INBOX;
subject = "got SUV?";
}
)
Type: Super-secret Automator pasteboard type (
{
account = Gmail;
id = 6622;
mailbox = INBOX;
subject = "got SUV?";
}
)
Type: dyn.ah62d4rv4gu8yc6durvwwa3xmrvw1gkdusm1044pxqyuha2pxsvw0e55bsmwca7d3sbwu (
eml
)
Type: Apple files promise pasteboard type (
eml
)
Type: public.url (null)
Type: CorePasteboardFlavorType 0x75726C20 (null)
Type: dyn.ah62d4rv4gu8yc6durvwwaznwmuuha2pxsvw0e55bsmwca7d3sbwu (
"message:%3C1a392cb9-b78c-402d-8d7f-dbd1d4a9ec42#Neptune.do.losaltos.k12.ca.us%3E",
""
)
Type: Apple URL pasteboard type (
"message:%3C1a392cb9-b78c-402d-8d7f-dbd1d4a9ec42#Neptune.do.losaltos.k12.ca.us%3E",
""
)
Type: public.url-name (null)
Type: CorePasteboardFlavorType 0x75726C6E (null)
Type: com.apple.pasteboard.promised-file-content-type com.apple.mail.email
Type: com.apple.pasteboard.promised-file-url (null)
Type: dyn.ah62d4rv4gu8y6y4usm1044pxqzb085xyqz1hk64uqm10c6xenv61a3k (null)
Type: NSPromiseContentsPboardType (null)
Anyone could help me?
I just went through this process and it was pretty easy actually.
Look for "com.apple.pasteboard.promised-file-content-type = com.apple.mail.email" in the pasteboard types and if found simply call namesOfPromisedFilesDroppedAtDestination (from NSDraggingInfo) with the drop destination URL and the file will be created when the drag is finished.

How to reach NSDictionary properties that have a class?

I've created an NSMutableArray with a class "Adealer" for it's objects. In my "listVC" I populate all the properties (as shown below) within a loop, where the "Adealer" class is used to create objects.
I then transferred one object from that array via a segue to my "detailsVC". Here I first import Adealer class and when I call this in my "detailsVC";
NSLog(#"restGPSTransfer (NSDictionary) class = %#, content is = %#",[restGPSTransfer class],[restGPSTransfer description]);
I get this;
restGPSTransfer (NSDictionary) class = Adealer, content is =
theDealer object and it's properties:
dealerID = 7
dealerName = Uppsala Centrum Test
dealerAdressStreet = Dragarbrunnsgatan 55
dealerAdressZip = 75320
dealerAdressCity = Uppsala
dealerTel =
dealerMail =
dealerWeb = www.uppsala.se
dealerLogo =
dealerImages =
dealerText = Uppsala centrum
dealerProducts =
dealerLongitude = 17.63893
dealerLatitude = 59.85856
dealerDistance = (null) // This is ok
dealerDistance2 = 8586398.000000
So far so god, BUT I can't reach the above properties in a simple call like this in the "detailsVC" (?!);
self.labelRestName.text = [restGPSTransfer objectForKey:#"dealerName"];
...it crashes with the error: "-[Adealer objectForKey:]: unrecognized selector sent to instance 0x8a92db0" (then termination with InvalidArgumentExeption...)
What have I missed?
Much more detail can be found in my question here (which I still got no answers to!);
How do I get out data from my NSDirectory (not just a property typo)?
Thank's for any help :-)
It looks like memory issue,
Are you using ARC?
Could you add your dictionary allocation code?

Objective C - sudzc, returns empty ArrayOfString, but xml response contains data

I'm newbie in Objective C. I'm trying to consume an webservice with sudcz.
I've got and Webservice server created in PHP an MySQL and now I'd like to comunicate with the server and getting a relevant results.
When calling a method i'm getting corrent XML response from the server but returned object of type ArrayOfString seems to be empty. NSLog prints just <ArrayOfString></ArrayOfString>, i'm assuming sudzc wasn't albe to parse xml in to a aproporiate form therefore the returned object is empty, am I wrong ? or I just cant see or cope with the returned object ? Has someone got any clue ?
If any materials needed i'll post them.
Thx.
Added :
CXMLElementNode* element = [[Soap getNode: [doc rootElement] withName: #"Body"] childAtIndex:0];
if(deserializeTo == nil) {
output = [Soap deserialize:element];
} else {
NSLog(#"Deserialize to : %#", deserializeTo);
if([deserializeTo respondsToSelector: #selector(initWithNode:)])
//problematic section
element = [element childAtIndex:0];
NSLog(#"NSSstring element : %#", element);
output = [deserializeTo initWithNode: element];
} else {
NSString* value = [[[element childAtIndex:0] childAtIndex:0] stringValue];
NSLog(#"NSSstring value : %#", value);
output = [Soap convert: value toType: deserializeTo];
}
NSLog(#"output : %#", output);
}
I think the problematic section is where I putted comments problematic section. Because object element is xml element and after calling output = [deserializeTo initWithNode: element], nothing happens, NSLog(output) prints the same output as printed NSLog(deserializeTo) before calling message initWithNode: element on object deserializeTo
I suspect SoapObject instance method initWithNode that it doesn't work as it should, because
my deserializeTo is instance of class RSiArrayOfstring a that class is subset of SoapObject and it delegates this message to parent class (SoapObject) and the implementation of this message in SoapObject is :
// Called when initializing the object from a node
- (id) initWithNode: (CXMLNode*) node {
if(self = [self init]) {
}
return self;
}
i can't see any manipulation with argument node
OUTPUT from debugger during executing this code :
2012-02-08 20:22:03.166 URS[1170:b603] Deserialize to : <ArrayOfstring></ArrayOfstring>
2012-02-08 20:23:17.258 URS[1170:b603] NSSstring element : <CXMLElement 0x5d7b920 [0x59701b0] return <return arrayType="SOAP-ENC:Array[6]" type="SOAP-ENC:Array">< item arrayType="xsd:string[1]" type="SOAP-ENC:Array">< item type="xsd:string">1 - Elektroúdržba</item>< /item>< item arrayType="xsd:string[1]" type="SOAP-ENC:Array">< item type="xsd:string">2 - Zámočnícka dielňa< /item>< /item>< item arrayType="xsd:string[1]" type="SOAP-ENC:Array">< item type="xsd:string">3 - Údržba žeriavov</item>< /item>< item arrayType="xsd:string[1]" type="SOAP-ENC:Array">< item type="xsd:string">6 - Zámočnícká dielňa H4< /item>< /item>< item arrayType="xsd:string[1]" type="SOAP-ENC:Array">< item type="xsd:string">4 - Mechanici< /item>< /item>< item arrayType="xsd:string[1]" type="SOAP-ENC:Array">< item type="xsd:string">5 - Údržba CNC< /item>< /item>< /return>>
2012-02-08 20:56:11.353 URS[1170:b603] output : < ArrayOfstring>< /ArrayOfstring
Hi i just ran into the same issue.
I found that
[[Soap getNode: [doc rootElement] withName: #"Body"]
omits the namespace of tags. So in my case the XML part looks like this:
<SOAP-ENV:Body>....
Changing the above line to:
[[Soap getNode: [doc rootElement] withName: #"SOAP-ENV:Body"]
helped in my case. Allthough i am not sure if the namespace varies for different soap implementations.
cheers!