SORU
22 NİSAN 2011, Cuma


Nasıl NSCache kullanmak

Birinin önbelleği için NSCache kullanmak için nasıl bir örnek, bir dize verebilir misiniz? Ya da herkes iyi bir açıklama için bir bağlantı vardır? Herhangi bulmak için görünmüyor olabilir

CEVAP
22 NİSAN 2011, Cuma


NSMutableDictionary kullanmak istiyorsunuz aynı şekilde kullanın. Fark NSCache aşırı bellek baskısı (yani çok fazla değer önbellekleme) algıladığında bu değerler biraz yer açmak için onu serbest bırakacak.

Zamanında bu değerleri yeniden yapabilirsiniz (ne olursa olsun İnternet, hesaplamalar yaparak, yükleyerek) NSCache ihtiyaçlarınıza uygun olabilir. Eğer verileri yeniden (örneğin, kullanıcı girişi, zamana duyarlı olduğunu, vb.) olamaz ise sonra orada telef olacak çünkü NSCache bir mağaza değil.

Örneğin, iş parçacığı güvenliğini dikkate alarak:

// Your cache should have a lifetime beyond the method or handful of methods
// that use it. For example, you could make it a field of your application
// delegate, or of your view controller, or something like that. Up to you.
NSCache *myCache = ...;
NSAssert(myCache != nil, @"cache object is missing");

// Try to get the existing object out of the cache, if it's there.
Widget *myWidget = [myCache objectForKey: @"Important Widget"];
if (!myWidget) {
    // It's not in the cache yet, or has been removed. We have to
    // create it. Presumably, creation is an expensive operation,
    // which is why we cache the results. If creation is cheap, we
    // probably don't need to bother caching it. That's a design
    // decision you'll have to make yourself.
    myWidget = [[[Widget alloc] initExpensively] autorelease];

    // Put it in the cache. It will stay there as long as the OS
    // has room for it. It may be removed at any time, however,
    // at which point we'll have to create it again on next use.
    [myCache setObject: myWidget forKey: @"Important Widget"];
}

// myWidget should exist now either way. Use it here.
if (myWidget) {
    [myWidget runOrWhatever];
}

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • infiniTuts

    infiniTuts

    18 Ocak 2012
  • Miles Fisher

    Miles Fisher

    8 NİSAN 2009
  • theatre2film

    theatre2film

    12 NİSAN 2006