Ionic 处理大数据的离子应用程序的性能问题

u1ehiz5o  于 2023-02-20  发布在  Ionic
关注(0)|答案(1)|浏览(224)

我们有一个离子角应用程序,基本上是一个联系人管理应用程序。随着应用程序中联系人的增加,应用程序变得非常慢。点击,滚动一切都变得很慢。
该应用程序使用离线存储并将数据填充到一个数组中,该数组用于呈现卡片列表。因此,网络不是问题。
我有大约1200个联系人发生这个问题。请告诉我如何调试问题。
列表页面的代码如下所示:

<ng-container *ngIf="core.syncedCards != undefined && core.syncedCards.length > 0">
          <ng-container *ngFor="let card of core.syncedCards">
            <app-contact-card [card]="card"></app-contact-card>
          </ng-container>  
        </ng-container>

组件代码为:

<ion-card>
    <ion-item lines="none">
      <ion-label (click)="viewCard(card)">{{card.firstName}} {{card.lastName}}</ion-label>
      
      <ion-button fill="clear" slot="end" (click)="toggleFavorite(card)">
        <ion-icon slot="icon-only"   color="warning" name="star"></ion-icon>
      </ion-button>
    </ion-item>
    <ion-card-subtitle>{{getJobDept()}}</ion-card-subtitle>
    <ion-card-content>
      <ion-item (click)="viewCard(card)">
          <ion-thumbnail slot="start" class="profile-pic">
            <img src="assets/images/noPerson.jpg">
          </ion-thumbnail>
          <ion-label class="ion-text-wrap">
             <ion-row><ion-col  class="ion-no-padding">{{card.company}}</ion-col></ion-row>
             <ion-row><ion-col  class="ion-no-padding">{{card.city}}, {{card.state}}</ion-col></ion-row>
             
             <ion-row *ngIf="card.tags !== undefined && card.tags.length > 0">
               <ion-col size="auto" *ngFor="let tag of card.tags">
                  <ion-chip [color]="core.getTagColor(tag)">
                    {{tag}}
                  </ion-chip>
               </ion-col>
             </ion-row>
          </ion-label>
        </ion-item>
        <ion-row class="ion-padding" style="padding-bottom:0px" *ngIf="refreshActions() === true"> 
          <ng-container *ngFor="let action of cardActions;let i = index">
            <ion-col size="2" *ngIf="i < 3">
              <ion-icon [name]="getLogoName(action.type)" [color]="getLogoColor(action.type)" size="large" (click)="doCardAction(action.type, card)"></ion-icon>
            </ion-col>
          </ng-container>
          <ion-col size="auto" *ngIf="cardActions.length > 3">
            <ion-icon name="chevron-down-outline" color="secondary" size="large" (click)="expandActions()"></ion-icon>
          </ion-col>
                  
           <ion-col class="ion-text-right" *ngIf="card.status == globalVariables.SYNC_FAILED">CRM Sync failed</ion-col>
           <ion-col class="ion-text-right ion-no-padding">
            <ion-icon name="ellipsis-vertical" size="large" (click)="showCardActions($event, card)" ></ion-icon>
           </ion-col>
        </ion-row>
        <ng-container *ngIf="showMoreActions === true">
            <ion-row class="ion-padding" style="padding-bottom:0px">
              <ng-container *ngFor="let action of cardActions;let i = index">
              <ion-col size="2" *ngIf="i > 2">
                <ion-icon [name]="getLogoName(action.type)" [color]="getLogoColor(action.type)" size="large" (click)="doCardAction(action.type, card)"></ion-icon>
              </ion-col>
            </ng-container>
            </ion-row>
        </ng-container>
       
  </ion-card-content>
  </ion-card>

填充syncedCards的代码如下所示:

const sCards = await this.storage.get("syncedCards")
          this.core.syncedCards = []
          console.log("offline synced cards are:", this.core.syncedCards)
          for(const card of sCards){
            this.core.syncedCards.push(this.core.copyContactDataFromJson(card))
          }

copyContactDataFromJson(cardQ: any) {
    var card = new Card(cardQ._tempContactId, cardQ._frontImageUrl, cardQ._backImageUrl, 
 cardQ._status, cardQ._date, cardQ._userId, cardQ._message, cardQ._objectType)
    card.firstName = cardQ._firstName
    card.lastName = cardQ._lastName
    card.jobTitle = cardQ._jobTitle
    card.company = cardQ._company
    card.email = cardQ._email
    card.mobile = cardQ._mobile
    card.workPhone = cardQ._workPhone
    card.website = cardQ._website
    card.street = cardQ._street
    card.city = cardQ._city
    card.state = cardQ._state
    card.country = cardQ._country
    card.postalCode = cardQ._postalCode
    card.favoriteContact = cardQ._favoriteContact
    card.profilePic = cardQ._profilePic
    card.dept = cardQ._dept
    card.fax = cardQ._fax
    card.workPhoneExtn = cardQ._workPhoneExtn
    card.buddyCode = cardQ._buddyCode
      card.buddyCodeLower = card.buddyCode.toLowerCase()
      card.mobileCountryCode = cardQ._mobileCountryCode
      card.mobileAreaCode = cardQ._mobileAreaCode
      card.mobileNumber = cardQ._mobileNumber
        card.mobileRawNumber = cardQ._mobileRawNumber
      card.workPhoneCountryCode = cardQ._workPhoneCountryCode
      card.workPhoneAreaCode = cardQ._workPhoneAreaCode
      card.workPhoneNumber = cardQ._workPhoneNumber
    card.workRawNumber = ''
      card.workRawNumber = cardQ._workRawNumber
      card.favoriteContact = false
      card.partyId = -1
      card.partyNumber = ''
      card.addressNumber = ''
      card.accountPartyNumber = cardQ._accountPartyNumber
      card.accountPartyId = cardQ._accountPartyId
      card.relationshipRecId = cardQ._relationshipRecId
      card.note = cardQ._note
      card.urlType = cardQ._urlType
    card.socialLinks = cardQ._socialLinks
      card.requireApproval = false

      card.tags = cardQ._tags
      card.phones = cardQ._phones

    return card
  }
uinbv5nw

uinbv5nw1#

在我看来,问题是你加载了太多的项目,即使你的手机屏幕不允许可视化每一个项目,它们也会降低你的应用程序的速度。
您有两种解决方案:

      • 如果联系人不会继续增加:**我建议你使用cdk-virtual-scroll-viewportdocumentation)来延迟加载元素。它只呈现适合屏幕的项目。

示例:

<cdk-virtual-scroll-viewport [itemSize]="userItemHeight" minBufferPx="400" maxBufferPx="800" #usersVirtualScroll>
    <ion-list>
      <app-favourites-reorder [favouriteUsers]="allUsersList.favourites" (movedList)="saveReorderFavourites($event)" *ngIf="reorderFavouriteUsers"></app-favourites-reorder>

      <div *cdkVirtualFor="let user of users">
        <app-user-item [user]="user">
        </app-user-item>
      </div>
    </ion-list>
</cdk-virtual-scroll-viewport>
      • 如果联系人不断增加:**您可以使用ion-infinite-scrolldocumentation)从数据库中一点一点地获取联系人,而不是一次获取所有联系人。现在您有1,200个联系人,但请考虑当您有1,000,000个联系人时。

示例:

<ion-content>
  <ion-list>
    <ion-item *ngFor="let item of items; let index">
      <ion-avatar slot="start">
        <img [src]="'https://picsum.photos/80/80?random=' + index" alt="avatar" />
      </ion-avatar>
      <ion-label>{{ item }}</ion-label>
    </ion-item>
  </ion-list>
  <ion-infinite-scroll (ionInfinite)="onIonInfinite($event)">
    <ion-infinite-scroll-content></ion-infinite-scroll-content>
  </ion-infinite-scroll>
</ion-content>

相关问题