Handle low storage space condition
Hi All,
I want to handle low storage space condition(isolated storage not ram memory) in my application. So if this case happens do i get some notification from system? Or do i need to set up a timer to check this every now and then myself. Searching for this i got only about handling low ram memory conditions. Please help
Regards
Re: Handle low storage space condition
Hello Kunal as far as I know, there is [I]IsolatedStorageFile.GetUserStoreForApplication().AvailableFreeSpace[/I] which return the available space on the phone, and [I]
IsolatedStorageFile.GetUserStoreForApplication().IncreaseQuotaTo()[/I] which helps increase the quota allowed (returns a bool, true if increased else it returns false)
Re: Handle low storage space condition
Hi Loukt,
So i need to check and handle these conditions by myself using timer or some other logic right.? System will not convey me by raising some event when [B]IsolatedStorageFile.GetUserStoreForApplication().AvailableFreeSpace[/B] becomes low.
Regards
Re: Handle low storage space condition
[QUOTE=Kunal Prime;913176]Hi Loukt,
So i need to check and handle these conditions by myself using timer or some other logic right.? System will not convey me by raising some event when [B]IsolatedStorageFile.GetUserStoreForApplication().AvailableFreeSpace[/B] becomes low.
Regards[/QUOTE]
Hello Kunal,
not necessary a timer, but each time you want to write on the IsolatedStorageFile
use this snippet to check if there is enough disk space, (I think neededSpace needs to be the byte value, as for 1Mb => 1*1024*1024 bytes )
[code]public void CheckDiskSpace(long neededSpace)
{
using (IsolatedStorageFile myISFile = IsolatedStorageFile.GetUserStoreForApplication())
{
if (myISFile.AvailableFreeSpace > neededSpace)
{
if (!myISFile.IncreaseQuotaTo(myISFile.Quota + neededSpace)) // newSpace = oldSpace(myISFile.Quota) + neededSpace
{
throw new Exception("No space available on the phone, please delete a file");
}
}
}
}[/code]
I've just wrote it and didn't test it yet, you can run it and give us feedback.
Yassine,
Re: Handle low storage space condition
Hi Yassine,
Thanks, will test this snippet and let you know. :)
Regards
Re: Handle low storage space condition
Hi Yassine,
Used below code
[QUOTE]using (IsolatedStorageFile myISFile = IsolatedStorageFile.GetUserStoreForApplication())
{
if (myISFile.AvailableFreeSpace < neededSpace)
{
long newSpace = myISFile.Quota + 10485760L;
if (!myISFile.IncreaseQuotaTo(newSpace))
{
throw new Exception("No space available on the phone, please delete a file");
}
else
{
Debug.WriteLine(myISFile.Quota);
}
}
}[/QUOTE]
But it throws me below exception.
[QUOTE]{System.ArgumentException: The new quota must be larger than the old quota.
at System.IO.IsolatedStorage.IsolatedStorageFile.IncreaseQuotaTo(Int64 newQuotaSize)
at SharedCode.IsolatedStorageAccess.FileStore.CheckDiskSpace(Int64 neededSpace)
at Marketplace.LandingPage..ctor()} System.Exception {System.ArgumentException}[/QUOTE]
While debugging i found myISFile.Quota returns 9223372036854775807 which is the maximum possible value for a signed long. So when we add anything to it appends a minus sign to indicate a bigger number. So initially myISFile.Quota = 9223372036854775807 and after adding 10MB, newSpace = -9223372036844290049. Which myISFile.IncreaseQuotaTo considers less than earlier quota and throws the exception. :P
Any thoughts or suggestions on this.??
And sorry i don't know how to embed code in reply so putting them in [QUOTE] tags.
Regards
Re: Handle low storage space condition
Hello Kunal (you just need to put your code in [CODE] tags),
I've just tryed the snippet, and yes I get the same result as you, first you need to know that Quota returns the maximum
after a small search I found this : "Windows Phone apps are not restricted to a particular quota. They should make careful use of storage based on their app scenario requirements."
that's why we get 9223372036854775807 as a Quota.
that snippet would work fine on a Silverlight app, but in the WP case there is no need, so basically since the Quota is not restricted all you'll have to do is to see if there is available space on the phone.
[code]public void CheckDiskSpace(long neededSpace)
{
using (IsolatedStorageFile myISFile = IsolatedStorageFile.GetUserStoreForApplication())
{
if (myISFile.AvailableFreeSpace < neededSpace) // if true, means there is no space available in the phone.
{
throw new Exception("No space available on the phone, please delete a file");
}
}
}[/code]
Re: Handle low storage space condition
Hi Yassine,
Ya i did that in meantime and arrived at the same code. :) BTW thanks for the [CODE] tag.:)
Regards
Re: Handle low storage space condition
Interesting question - Yassine, possibly a good example for a FAQ? :-)
I've created a preliminary howto on this here: [URL="http://www.developer.nokia.com/Community/Wiki/How_to_check_for_available_storage_space_on_Windows_Phone"]How_to_check_for_available_storage_space_on_Windows_Phone[/URL]. Can you please check it. Note, normally I'd link to the official IsolatedStorageFile Howtos, but now most of these have been removed because this is a deprecated API for WP8.
A few questions.
1. Why check for availability rather than doing try-catch?
2. In this case we're after something that works for WP7 and 8, so IsolatedStorageFile is best. However going forward people are supposed to use [URL="http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.aspx"]Windows.Storage[/URL]. What would be the correct code for WP8 and later?
Anyone game to create some example code we can attach.
This is still in draft while we discuss.
Re: Handle low storage space condition
Hi hamishwillee,
Answering your questions.
1. Why check for availability rather than doing try-catch?
Ans. Say you have very important data in memory which should never be lost in any case. Say you need atleast "X"MB of space on disk to save that data. So you can keep checking for available space at regular intervals (maybe increase that interval when free space becomes < 100MB) and the moment available space goes less than "X+2"MB save the data and convey user of low storage space. But if you use a try catch block and you get an exception, which means there is no more space to store data. In this scenario max you can do is to convey user. Now if user do not free some space your data is doomed.
2. In this case we're after something that works for WP7 and 8, so IsolatedStorageFile is best. However going forward people are supposed to use Windows.Storage. What would be the correct code for WP8 and later?
Maybe Yassine is better candidate to answer this question.
Regards
Re: Handle low storage space condition
Well, if you roughly know how large a dataset can get you could check upon creation wether there is enough space available. E.g. if you take a photo you could do the storage space check when opening the ViewFinder and prevent the user from taking pictures he can't store later on. I believe it would be best to tie the checks to certain actions rather than to some kind of "timer".
try/catch should still be used so the user can be informed about what went wrong in case the estimate of how much space would be required was wrong, etc.
Re: Handle low storage space condition
Hi SB Dev,
[QUOTE=SB Dev;914046]Well, if you roughly know how large a dataset can get you could check upon creation wether there is enough space available.[/QUOTE]
In my case its not quite possible to estimate data set size. It completely depends on user actions. But yes, if that's possible then checking available space at start up will be very useful. Good point.
[QUOTE=SB Dev;914046]I believe it would be best to tie the checks to certain actions rather than to some kind of "timer".[/QUOTE]
Absolutely. Even i did it on user actions.
Regards
Re: Handle low storage space condition
For the first one, if my app uses Isolated Storage a lot I will check the availability in the app launch, why ? simply to show the user a message telling him that the phone is low on memory or after every change happended on the isolated storage.
Regarding the second one, I made a little search and I found no method for retrieving disk space in [B]Windows.Storage[/B] even if it is used by WP8 and WinRT apps. WinRT developpers are using a special function found in [B]kernet32.dll[/B] [i]GetDiskFreeSpaceEx[/i], and as written in the [URL="http://msdn.microsoft.com/en-us/library/windows/desktop/aa364937(v=vs.85).aspx"]docs[/URL], this api is supported in Windows Phone 8, which I found doubtful since you need to give the disk name as a parameter