简体   繁体   中英

Method returns NSString as nil

I have method which builds NSString and returns it correctly. However if i call the same method from separate thread it returns nil even though my NSString is not nil in line with return.

I get request = nil in "getPhones" method.

"return request" in "createSelectPhonesRequest" ontains valid string.

"getXmlRow" actually builds the string.

+ (void)getUserPhones:(User *)user
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    [DataManager getPhones:user];

    [pool release];
}

+ (void)getPhones:(User*)user
{
    NSString *request = [XMLBuilder createSelectPhonesRequest:user.NickName];
    NSString *getAddressesResponce = [TrafficManager sendRequest:request];      
}


+(NSString*) createSelectPhonesRequest:(NSString*)nickName
{
    NSString *request;
    NSMutableArray *attributes;
    NSMutableArray *attributesValues;

    /*** root ***/
    attributes = [NSMutableArray new];
    attributesValues = [NSMutableArray new];

    //time
    [attributes addObject:xmlAt.time];
    [attributesValues addObject:[NSDate getXmlFormattedDateFromCurrentDate]];
    //action
    [attributes addObject:xmlAt.action];    
    [attributesValues addObject:xmlAV.selectPhones];


    request = [XMLBuilder getXmlRow:xmlEl.envelop 
                         attributes:attributes 
                   attributesValues:attributesValues 
                              value:nil 
                              close:YES];
    [attributes release];
    [attributesValues release];


    return request;
}


+(NSString*) getXmlRow:(NSString*)element 
               attributes:(NSMutableArray*)attributes 
         attributesValues:(NSMutableArray*)attributesValues 
                    value:(NSString*)value 
                    close:(BOOL)close

{
    //open tag
    NSString* xmlRow = @"<";    
    //tag
    xmlRow = [xmlRow stringByAppendingString:element];
    //attrivutes+values
    if (attributes && attributesValues)
        for (int i=0; i<[attributes count]; i++)
        {
            NSString *attribute = (NSString*) [attributes objectAtIndex:i];
            NSString *attributeValue = (NSString*) [attributesValues objectAtIndex:i];

            xmlRow = [xmlRow stringByAppendingString:@" "];
            xmlRow = [xmlRow stringByAppendingString:(attribute)];      
            xmlRow = [xmlRow stringByAppendingString:@"=\""];
            xmlRow = [xmlRow stringByAppendingString:attributeValue];       
            xmlRow = [xmlRow stringByAppendingString:@"\""];
        }
    //end tag
    xmlRow = [xmlRow stringByAppendingString:@">"];
    //value
    if (value != nil)
        xmlRow = [xmlRow stringByAppendingString:value];
    //close tag
    if (close)
        xmlRow = [xmlRow stringByAppendingString:[self closeElement:element]];
    return xmlRow;
}

I suspect that you have not created autorelease pool for the thread. Every thread must have it's own autorelease pool.

我认为您应该像这样正确初始化字符串,

NSString *myString = [NSString stringWithString:@"Hello"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM