Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Publish及Subscribe:使用RProperty进行subscribe操作

From Wiki
Jump to: navigation, search
Article Metadata

兼容于
文章
翻译:
hoolee
最后由 hamishwillee 在 14 Sep 2012 编辑


Contents

详细描述

下列代码片段演示了如何使用RProperty来订阅用户使用Symbian IPC发布及订阅机制定义的属性。下列步骤用来订阅并接收属性更新状态。

  • 使用RProperty::Attach()标注需要的属性,然后调用RProperty::Subscribe()订阅
  • 通过TRequestStatus信号通知RProperty::Subscribe()完成。
  • 调用RProperty::Get()获得更新后的属性值
  • 通过RProperty::Subscribe重新提交请求来监听属性变化
  • 最终调用RProperty::Cancel来取消订阅,以及通过RProperty::Close

Archived:Using RProperty for publishing中的代码片段演示了用户定义属性如何被发布,这些属性定义将通过ExampleProperties.h在发布者和订阅者之间共享。

#ifndef EXAMPLEPROPERTIES_H_
#define EXAMPLEPROPERTIES_H_
 
const TInt KMaxStrLen = 10;
const TUid KExampleProperty = {0xED1917A8};
enum TExamplePropertyKeys { EIntProperty, EStrProperty };
 
#endif /*EXAMPLEPROPERTIES_H_*/

This snippet can be self-signed.

MMP文件

需要下列链接库

LIBRARY euser.lib

前言

发布者或订阅者线程都可以调用RProperty::Define()来定义属性。用户定义的属性将一直保存到操作系统重启或被删除。

TInt ret=RProperty::Define(KExampleProperty,EIntProperty,RProperty::EInt);
if (ret != KErrAlreadyExists)
{
User::LeaveIfError(ret);
}
 
ret= RProperty::Define(KExampleProperty,EStrProperty,RProperty::EByteArray,KMaxStrLen);
if (ret != KErrAlreadyExists)
{
User::LeaveIfError(ret);
}

头文件

#ifndef EXAMPLESUBSCRIBER_H_
#define EXAMPLESUBSCRIBER_H_
 
// INCLUDES
#include <e32base.h>
#include <e32property.h>
 
#include "exampleproperties.h"
 
 
class MExampleSubscriberObserver
{
public:
virtual void IntPropertyUpdatedL(TInt aValue) = 0;
virtual void StrPropertyUpdatedL(TDes& aValue) = 0;
};
 
 
class CExampleSubscriber : public CActive
{
enum {EPriority=0};
public:
static CExampleSubscriber* NewL( const TUid aUid,
const TUint32 aKey,
MExampleSubscriberObserver& aNotifier );
virtual ~CExampleSubscriber();
 
private:
CExampleSubscriber( const TUid aUid, const TUint32 aKey,
MExampleSubscriberObserver& aNotifier );
void ConstructL();
void RunL();
void DoCancel();
private:
RProperty iProperty;
const TUid iUid;
const TUint32 iKey;
MExampleSubscriberObserver& iObserver;
};
 
#endif /*EXAMPLESUBSCRIBER_H_*/

源文件

#include "ExampleSubscriber.h"
 
CExampleSubscriber::CExampleSubscriber(
const TUid aUid, const TUint32 aKey,
MExampleSubscriberObserver& aObserver )
: CActive(EPriority), iUid( aUid ),
iKey( aKey ), iObserver( aObserver)
{
}
 
CExampleSubscriber* CExampleSubscriber::NewL( const TUid aUid, const TUint32 aKey,
MExampleSubscriberObserver& aObserver )
{
CExampleSubscriber* self=
new(ELeave) CExampleSubscriber( aUid, aKey, aObserver );
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
 
void CExampleSubscriber::ConstructL()
{
User::LeaveIfError( iProperty.Attach( iUid, iKey ) );
CActiveScheduler::Add(this);
// initial subscription and process current property value
RunL();
}
 
CExampleSubscriber::~CExampleSubscriber()
{
Cancel();
iProperty.Close();
}
 
void CExampleSubscriber::DoCancel()
{
iProperty.Cancel();
}
 
void CExampleSubscriber::RunL()
{
// resubscribe before processing new value to prevent missing updates
iProperty.Subscribe( iStatus );
SetActive();
 
TInt intValue;
TBuf<KMaxStrLen> strValue;
 
switch( iKey )
{
case EIntProperty:
// Int property updated, get new value
//see also: RProperty::Get(KExampleProperty,EIntProperty,intValue);
if( iProperty.Get( intValue ) == KErrNotFound )
{
// Int property deleted, do necessary actions here...
}
else
{
// use new value ...
iObserver.IntPropertyUpdatedL(intValue);
}
break;
case EStrProperty:
// Int property updated, get new value
//see also: RProperty::Get(KExampleProperty,EStrProperty,strValue);
if( iProperty.Get( strValue ) == KErrNotFound )
{
// Str property deleted, do necessary actions here...
}
else
{
// use new value ...
iObserver.StrPropertyUpdatedL(strValue);
}
break;
 
default:
break;
}
}
 
// End of File


使用CExampleSubscriber类

  • 在头文件里:
#include "examplesubscriber.h"
 
 
class CPropertyExampleAppUi : public CAknAppUi, MExampleSubscriberObserver
{
//...
public:
void IntPropertyUpdatedL(TInt aValue);
void StrPropertyUpdatedL(TDes& aValue);
//...
CExampleSubscriber* iIntSubscriber;
CExampleSubscriber* iStrSubscriber;
};
  • In the source file:
void CPropertyExampleAppUi::ConstructL()
{
//...
iIntSubscriber = CExampleSubscriber::NewL(KExampleProperty,EIntProperty,*this);
iStrSubscriber = CExampleSubscriber::NewL(KExampleProperty,EStrProperty,*this);
}
 
CPropertyExampleAppUi::~CPropertyExampleAppUi()
{
//...
delete iIntSubscriber;
delete iStrSubscriber;
}
 
void CPropertyExampleAppUi::IntPropertyUpdatedL(TInt aValue)
{
//do something with the updated property
}
 
void CPropertyExampleAppUi::StrPropertyUpdatedL(TDes& aValue)
{
//do something with the updated property
}

后记

使用RProperty来订阅两个示例属性

如果订阅线程定义了属性,那么也可以使用RProperty::Delete()来删除掉。

TInt err(KErrNone);
 
err = RProperty::Delete(KExampleProperty,EIntProperty);
if (err != KErrNotFound)
{
User::LeaveIfError(err);
}
err = RProperty::Delete(KExampleProperty,EStrProperty);
if (err != KErrNotFound)
{
User::LeaveIfError(err);
}

当属性都被删除掉后,任何未完成的订阅将被告知KErrNotFound完成

相关文档

Archived:Using RProperty for publishing

This page was last modified on 14 September 2012, at 06:07.
346 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×