Wednesday, May 16, 2012

Leave object in cache unchanged

this code works "perfectly". Creates an XDocument, puts it in cache, and retrieves it the next time round.



BUT i'd like the object in cache NOT to be affected by the changes made after the marked place, where I add some elements only valid in this session.



The object in cache is relevant for all users on the page, so should only be genereted/altered once in cache lifetime.



I COULD clone it from the cache or store it as a string I guess (not a reference type) and losd into new XDocument, but what would be the most gentle way? Seems overkill, when I can cache the XDocument as it is...



Thanks in advance,



Steen



{
string cachekey = "categories";
var cateGoryDoc = HttpRuntime.Cache.Get(cachekey) as XDocument;

if (cateGoryDoc == null)
{
XDocument doc = XDocument.Parse(this.ProductList1.getXML());
IEnumerable<XElement> mainCats =
from Category1 in doc.Descendants("product").Descendants("category") select Category1;
var cDoc = new XDocument(new XDeclaration("1.0", "utf-8", null), new XElement("root"));
cDoc.Root.Add(mainCats);
cateGoryDoc = cDoc;
HttpRuntime.Cache.Insert(cachekey, cDoc, null,
DateTime.Now.AddMinutes(10),
TimeSpan.Zero);
}


// I DO NOT WANT TO CHANGE THE DOC IN CACHE!!
if (HttpContext.Current.Request["tCats"] != null)
{
string[] tCats = HttpContext.Current.Request["tCats"].Split(Convert.ToChar(","));

foreach (string tCat in tCats)
{
cateGoryDoc.Root.Add(new XElement("selectedCategory", tCat));
}
}

this.catecoryXML.DocumentContent = cateGoryDoc.ToString();
this.catecoryXML.TransformSource = "/XSLTFile1.xslt";
}




Train voice recognition

I have built a voice recognition system in C# and I'm using the Microsoft Speech Platform 11.0 (Swedish language packs). I use a wav file as an input for the SpeechRecognitionEngine.



The problem is that some of the words (40%) are not recognized at all.



I would like to record some commands (a limited number of Swedish words and/or numbers) to a sound file and import them so that the SpeechRecognitionEngine could be able to understand them.



For example:



Record when an user says the word: "Katt" (Swedish word for cat) and then be able to tell the RecognitionEngine that this means "Katt".



Is this possible or are there better solutions?





Is Online Gambling getting more popular and is there a website where one could gamble?

Is Online Gambling getting more popular and is there a website where one could gamble?





Why does SQL display an & as &amp;?

I need some assistance, my query is below:



STUFF(
(
SELECT ',' + CountDesc
FROM Count INNER JOIN ProjectCount ON Count.Id = ProjectCount.CountId
WHERE ProjectCount.ProjectId = Project.Id ORDER BY Count.CountDesc
FOR XML PATH('')
), 1, 1, '') as [Country]


What happens is when i run this query and the Count table has an & in one of its fields, it displays the & &



Is there anyway to not let this happen?



Thanks in advance.





Accidentally merged other branches when using `git pull` with no arguments

Not knowing any better, I'd always used blissfully git pull (without arguments) to get remote updates.



Now that we're using branches, this has come back to haunt me.



After doing a git pull, I noticed that my local master had been merged with the other branches as well.



I'm trying to figure out how this happened so that I can avoid doing it again in the future, but no luck so far.



I didn't put anything strange in my .git/config file.





findAndResignFirstResponder not dismissing keyboard

I have 6 UITextFields created in Interface Builder. After entering the values, and clicking the search button, I need to dismiss the keyboard from the view.
Currently I'm using 'findAndResignFirstResponder' and have delegates set for all text fields however the keyboard is not dismissing.



Any idea why this isn't working? Is there a better way to dismiss the keyboard?





How to let user change iphone app theme?

I have Article and Category entities that have a many to many relationship. Category has a name and a imagePath (which keep its name and path to image of an icon for that category).
If I want to let user change icons (for example change theme with a new pack of icon) what should I do?



What I'm thinking now is create separate entity, called Theme, which keep categoryId (need to add that to category) and move imagePath from Category to this Theme entity, so it would become



Article <<->> Category  
..... - categoryId
..... - name


and



Theme
-categoryId
-imagePath


with this approach I have to keep the selected Theme in NSUserDefault and lookup with categoryId every time I need an image.



Any thing I should concern with this or there is a better way of doing this kind of thing?



Edited



I will let user choose theme and save it in NSUserDefaults (just like normal setting preference), then when I want to show it for Article I get categoryId from Category and use that categoryId to search in Theme entity, get the imagePath and load image from that path. Here is some NSPredicate I think of



NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Theme" inManagedObjectContext:moc];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"categoryId == %@ AND themeId = %@", category_id_for_article, theme_id_from_user_defaults];
[request setPredicate:predicate];