TempData keep() vs peek()
Asked Answered
H

5

92

What is the difference between keep() and peek()?

MSDN says:

  • keep(): marks the specified key in the dictionary for retention.
  • peek(): returns an object that contains the element that is associated with the specified key, without marking the key for deletion.

I can't get really what the difference is, don't they both keep a value for another request?

Heavyweight answered 21/1, 2014 at 8:33 Comment(1)
Peek() allows you to read an item without marking it for deletion while Keep() allows you to Revive either all values in the TempData dictionary (using Keep()) or a single item (using Keep("Key")) that's been marked for deletion. I use Keep() when there's a specific criteria in the logical flow that creates the need to retain TempData that should be otherwise (under normal circumstances) cleared. e.g.: var query = TempData["Query"]; /* then somewhere down the road */ if(keepQueryForSomeReason) TempData.Keep("Query"); // this revives Query from "marked for deleted" stateDaveta
B
171

When an object in a TempDataDictionary is read, it will be marked for deletion at the end of that request.

That means if you put something on TempData like

TempData["value"] = "someValueForNextRequest";

And on another request you access it, the value will be there but as soon as you read it, the value will be marked for deletion:

//second request, read value and is marked for deletion
object value = TempData["value"];

//third request, value is not there as it was deleted at the end of the second request
TempData["value"] == null

The Peek and Keep methods allow you to read the value without marking it for deletion. Say we get back to the first request where the value was saved to TempData.

With Peek you get the value without marking it for deletion with a single call, see msdn:

//second request, PEEK value so it is not deleted at the end of the request
object value = TempData.Peek("value");

//third request, read value and mark it for deletion
object value = TempData["value"];

With Keep you specify a key that was marked for deletion that you want to keep. Retrieving the object and later on saving it from deletion are 2 different calls. See msdn

//second request, get value marking it from deletion
object value = TempData["value"];
//later on decide to keep it
TempData.Keep("value");

//third request, read value and mark it for deletion
object value = TempData["value"];

You can use Peek when you always want to retain the value for another request. Use Keep when retaining the value depends on additional logic.

You have 2 good questions about how TempData works here and here

Hope it helps!

Bacillus answered 21/1, 2014 at 9:34 Comment(2)
When does the delete actually occur? So if it is marked for deletion after a read, and then I do some other stuff, and decide to use a keep, will it be guaranteed to be around as long as I have initiated a redirect?Cayman
It will be deleted at the end of the current request, after the result and all filters have been executed. This is as long as you use the base controller class, check for usages of TempData.Save thereBacillus
T
57

Just finished understanding Peek and Keep and had same confusion initially. The confusion arises becauses TempData behaves differently under different condition. You can watch this video which explains the Keep and Peek with demonstration https://www.facebook.com/video.php?v=689393794478113

Tempdata helps to preserve values for a single request and CAN ALSO preserve values for the next request depending on 4 conditions”.

If we understand these 4 points you would see more clarity.Below is a diagram with all 4 conditions, read the third and fourth point which talks about Peek and Keep.

enter image description here

Condition 1 (Not read):- If you set a “TempData” inside your action and if you do not read it in your view then “TempData” will be persisted for the next request.

Condition 2 ( Normal Read) :- If you read the “TempData” normally like the below code it will not persist for the next request.

string str = TempData["MyData"];

Even if you are displaying it’s a normal read like the code below.

@TempData["MyData"];

Condition 3 (Read and Keep) :- If you read the “TempData” and call the “Keep” method it will be persisted.

@TempData["MyData"];
TempData.Keep("MyData");

Condition 4 ( Peek and Read) :- If you read “TempData” by using the “Peek” method it will persist for the next request.

string str = TempData.Peek("Td").ToString();

Reference :- http://www.codeproject.com/Articles/818493/MVC-Tempdata-Peek-and-Keep-confusion

Tarweed answered 14/9, 2014 at 1:0 Comment(6)
so that means, “Peek” method will persist for the next request and “Keep” method will not be persisted for next request or third request, am i right ?Worriment
@stom Peek mark the key for retention so the data will be persist till the next request.Nuisance
well explained.Moire
Within the same request can I read the same TempData key multiple times?Unconformable
in short, Peek = Read + KeepContort
That Facebook video has no audio, but it should. Here is the link for it on YouTube with audio: youtube.com/watch?v=4PWq_nQZcfY And, there are better videos explaining this on YouTube as well.Talbot
A
7

TempData is also a dictionary object that stays for the time of an HTTP Request. So, TempData can be used to maintain data between one controller action to the other controller action.

TempData is used to check the null values each time. TempData contain two method keep() and peek() for maintain data state from one controller action to others.

When TempDataDictionary object is read, At the end of request marks as deletion to current read object.

The keep() and peek() method is used to read the data without deletion the current read object.

You can use Peek() when you always want to hold/prevent the value for another request. You can use Keep() when prevent/hold the value depends on additional logic.

Overloading in TempData.Peek() & TempData.Keep() as given below.

TempData.Keep() have 2 overloaded methods.

  1. void keep() : That menace all the data not deleted on current request completion.

  2. void keep(string key) : persist the specific item in TempData with help of name.

TempData.Peek() no overloaded methods.

  1. object peek(string key) : return an object that contain items with specific key without making key for deletion.

Example for return type of TempData.Keep() & TempData.Peek() methods as given below.

public void Keep(string key) { _retainedKeys.Add(key); }

public object Peek(string key) { object value = values; return value; }

Accountancy answered 21/1, 2014 at 8:33 Comment(1)
Possible source for first paragraph: Top 10 ASP.NET MVC Interview Questions, "TempData is also a dictionary object that stays for the time of an HTTP Request. So, Tempdata can be used to maintain data between redirects, i.e., from one controller to the other controller."Borchert
A
2

don't they both keep a value for another request?

Yes they do, but when the first one is void, the second one returns and object:

public void Keep(string key)
{
    _retainedKeys.Add(key); // just adds the key to the collection for retention
}

public object Peek(string key)
{
    object value;
    _data.TryGetValue(key, out value);
    return value; // returns an object without marking it for deletion
}
Aroma answered 21/1, 2014 at 9:32 Comment(0)
S
-1

Keep() method marks the specified key in the dictionary for retention

You can use Keep() when prevent/hold the value depends on additional logic.

when you read TempData one’s and want to hold for another request then use keep method, so TempData can available for next request as above example.

Seaborne answered 8/9, 2019 at 12:34 Comment(1)
This answer - to an older questions with numerous useful and correct answer already - provides no additional value. Please refrain from such contributions that add nothig to existing content, especially on older questions that already accepted and useful answers.Potato

© 2022 - 2024 — McMap. All rights reserved.