Reputation: 172
I want to assign large amount of character from a file to a NSString. I did that. It is working fine for me in iOS simulator. But crashed in iOS devices. I think, length exceeds the NSString memory. I can realize that NSString limit will be change with different devices and available memory. My question, Is there any alternative to get the string from file and store it in the runtime with out memory issue?. or How to check NSString's available length?
Here my code. I am appending the parsed string in NSString
Str=[Str stringByAppendingString:TmpStr];
Upvotes: 1
Views: 290
Reputation: 39306
You may want to confirm it's actually a memory issue and handle the low memory condition. See this SO post on how to do that: iPhone - How to deal with low memory conditions
If you're trying to load a books content at once into an NSString it will likely run into out of memory conditions on memory constrained devices. A stream allows you to read the file a portion at a time and only hold the current portion of the file in memory. Even if you can successfully load some books completely, it will be slower and still consume much more memory than the app needs to - which will make your app one of the first to get flagged in low memory conditions.
Checkout Apple's Stream Programming Guide
From that guide:
Cocoa includes three stream-related classes: NSStream, NSInputStream, and NSOutputStream. NSStream is an abstract class that defines the fundamental interface and properties for all stream objects. NSInputStream and NSOutputStream are subclasses of NSStream and implement default input-stream and output-stream behavior. You can create NSOutputStream instances for stream data located in memory or written to a file or C buffer; you can create NSInputStream instances for stream data read from an NSData object or a file.
EDIT: In the comment you clarified you're loading from XML. You still have the same fundamental issue, you're trying to load large contents of some sort of "book" into memory at a time. You're appending tmp string as you parse the xml so the effect is the same. So, you have to find a way to incrementally load the portion of the book you need. Luckily, you can tell NSXMLParser to use a stream.
This may help: Using NSXMLParser initWithStream: no parser delegate methods received
I also just found this SO post on the topic that may help: Objective-C: Reading a file line by line
Not sure about the details in your app but you could stream up to the current data you need (and a few pages on either side) or you could even stream and break apart the contents into "pages" in some temp files on disk as sort of a "pages cache".
Upvotes: 2
Reputation: 4656
Use NSMutableString instead of NSString. that might help You!
Upvotes: 0
Reputation: 840
Will you please share the console log when your app crashes? As much I think that's not the real problem, problem is different bit. Meantime, you can try by using the database for storage. And, if possible please show the line where you're trying to assign your string.
Upvotes: 0