iPhone: Storing audio data on NSUserDefaults and memory issues
<p>I am downloading several small size songs from server and storing(NSData) into NSUserDefaults, so that i can use it when needed to cache and play directly on the device, instead of downloading and playing again from server.
The problem, if i store few couple of smaller size songs as an data format in NSUserDefaults, it reduces lots of device memory and throwing memory warning or crashing etc.</p>

<p>Could you someone guide me how can i resolve it? How can i store song data persistently on the device for Cache purpose, and same time storing in less memory usage?</p>

<p><strong>UPDATED:</strong> As per the suggestion, i tried to add song data into dictionary as file and tried to retrieve it as below. But, still i'm facing the same issue, memory warning after around 30mb of data retrieved.. Could someone help me to resolve this? I need to store around 40 mb of song data and store it.</p>

<pre><code>    NSURL *songurl = [NSURL URLWithString:downloadSOngUrl];
            NSMutableData *songdata = [NSMutableData dataWithContentsOfURL:songurl];

NSString *fileName = [downloadSOngUrl lastPathComponent];
            NSLog(@"fileName: %@",fileName);

            [appDelegate writeSongIntoDocsDirectory :songdata :fileName];

-(void) writeSongIntoDocsDirectory :(NSData *) inSongData :(NSString *) songNamePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    NSLog(@"songNamePath: %@", songNamePath);

    songNamePath = [songNamePath stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] ];
    NSLog(@"songNamePath: %@", songNamePath);

    if ( [inSongData writeToFile:[documentsDirectory stringByAppendingPathComponent:songNamePath] atomically:YES] ) 
    {
        // Success !
        NSLog(@"Successfully saved the song into documents directory");

    } 
    else 
    {
        // Error !  
        NSLog(@"Error when Successfully saving song into documents directory");
    }
}
-(NSData *) readSongDataFromDocsDirectory :(NSString *) filePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    NSData *readData = [NSData dataWithContentsOfFile:[documentsDirectory stringByAppendingPathComponent:filePath]];

    return readData;
}
</code></pre>

<p>Thanks in advance!</p>
