Local Class Prefixes not working correctly!

0 views
Skip to first unread message

Kevin Elliott

unread,
Dec 23, 2009, 1:21:06 AM12/23/09
to ObjectiveResource
A few of my OR classes were using reserved/colliding names, so I
decided to take advantage of putting a prefix in front of all my OR
classes. So, I refactored all my classes by putting "WL" in front of
them and then updating any references to it.

Unfortunately this broke my working code by retrieving a whopping
total of 0 objects for any remote calls. Observing the server logs,
the requests are coming in correctly (without the prefix), however, 0
objects are returned in the NSArray. I have run this in the debugger,
putting a breakpoint on the parser:didStartElement method in the
FromXMLElementDelegate class, an area I suspected might be causing
this problem.

Sure enough, in the debugger, it shows that the parser is expecting
the remote XML name to match the name with the prefix (which, of
course it will not), and thus does not parse it:

(FromXMLElementDelegate.m:39)

if ([elementName isEqualToString:[self.targetClass xmlElementName]])
{

As this is an event driven XML parser, I'm certain this needs to be
fixed here and in the other event methods as well.

Why were local prefixes patched in without this? It seems very
incomplete by this not existing! Was the original code only written
for JSON parsing (thus not needing to match any names perhaps)? Has
anyone patched this further?

-Kevin

Kevin Elliott

unread,
Dec 23, 2009, 6:52:41 PM12/23/09
to ObjectiveResource
After many, many hours of debugging the XML parser in
ObjectiveResource (somewhat over my head originally), I've finally
come up with a minor patch solution.

If you replace line 59 of FromXMLElementDelegate.m:

Class elementClass = NSClassFromString([currentPropertyName
toClassName]);

With:

NSString *className = [currentPropertyName toClassName];
if ([self.targetClass getLocalClassesPrefix] != nil)
{
className = [NSString stringWithFormat:@"%@%@", [self.targetClass
getLocalClassesPrefix], className];
}
Class elementClass = NSClassFromString(className);

Then it will properly detect the class during XML parsing, and return
the correct object stack.

This assumes you have set the prefix with [ObjectiveResourceConfig
setLocalClassesPrefix:@"Foo"];

Additionally, you will need to have the xmlElementName method
overridden in your prefixed class to define the non-prefixed xml
element, such as (assuming your class is FooHome, where Foo is the
prefix):

+ (NSString *) xmlElementName
{
return @"home";
}

Not sure why this was left out to begin with (because parsing should
never work without it), unless I've completely mistaken the use of
this (very possible, but unlikely).

Hope this saves someone else days of time :)

-Kevin

Reply all
Reply to author
Forward
0 new messages